首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2.2+hibernate3.2+spring2调整入门实例

2012-08-29 
struts2.2+hibernate3.2+spring2整合入门实例环境:windows 7+MyEclipse 8.6服务器: Tomcat 6.0框架版本:st

struts2.2+hibernate3.2+spring2整合入门实例

环境:windows 7+MyEclipse 8.6

服务器: Tomcat 6.0

框架版本:struts2.2+hibernate3.2+spring 2.2

?

项目目录结构:

struts2.2+hibernate3.2+spring2调整入门实例

?

步骤一,添加框架支持的包:

struts2.2+hibernate3.2+spring2调整入门实例

struts2.2+hibernate3.2+spring2调整入门实例

struts2.2+hibernate3.2+spring2调整入门实例

步骤二:编写jsp页面:index.jsp:

?

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>Login</title>  </head>   <body>  <h2 style="color: red">Login</h2>     <s:form action="login" namespace="/user">      <s:textfield name="user.username" label="%{getText('username')}"> </s:textfield>      <s:password name="user.password" label="%{getText('password')}"></s:password>      <s:submit name="submit" value="%{getText('submit')}"></s:submit>      </s:form>  </body></html>
?

result.jsp:

<body>

? ?Login success!

?</body>

步骤三:编写web.xml文件:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <filter>  <filter-name>struts2</filter-name>  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>    <listener>        <listener-class>                org.springframework.web.context.ContextLoaderListener              </listener-class>   </listener>  </web-app>

?步骤四:建数据库,(本例用MySQL),编写映射文件User.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mappingPUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.gufengxiachen.s2sh.bean.User" table="user"  catalog="test"><id name="id" column="id" type="int"><generator column="username" type="string"></property><property name="password" column="password" type="string"></property></class></hibernate-mapping>

?步骤五:编写struts.xml文件:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd">     <struts> --><package name="s2sh" extends="struts-default" namespace="/user" > <action name="login" >  <result name="success">/result.jsp</result> <result name="input">/index.jsp</result>  </action>  </package> </struts>
?

步骤六:编写spring配置文件applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><bean id="dataSource" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/test"></property><property name="username" value="root"></property><property name="password" value=""></property><property name="maxActive" value="100"></property><property name="defaultAutoCommit" value="true"></property></bean><bean id="sessionFactory" ref="dataSource"></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop></props></property><property name="mappingResources"><list><value>com/gufengxiachen/s2sh/bean/User.hbm.xml</value></list></property></bean><bean id="userDao" scope="singleton"><property name="sessionFactory"><ref bean="sessionFactory"/></property></bean><bean id="userService" ref="userDao"></property></bean><bean id="loginAction" ref="userService"></property></bean></beans>
?步骤七:编写控制层Action,服务层(业务逻辑)Service,数据访问层,由于代码太多,详细代码见附件!

?

热点排行