环境: myeclipse10 ,mysql ,jdk6.0
目录结构:
一、新建web project
二、在src目录下新建struts.xml ,使用include是为了方便分开各个action
<?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> <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 开发模式下使用,这样可以打印出更详细的错误信息 --> <constant name="struts.devMode" value="true" /> <include file="admin.xml" /> </struts>
<?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="admin" extends="struts-default"> <action name="admin_*" class="adminAction" method="{1}"> <result name="login"> /backend/main.jsp <!-- 页面跳转 --> </result> <result name="delSuc" type="redirectAction"> <param name="actionName">admin_login</param> <!-- action跳转 --> </result> </action> </package> </struts>
三、在src目录增加hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <mapping resource="com/d3/d3studio/model/Admin.hbm.xml" /> </session-factory> </hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 指定连接数据库的JDBC驱动 --> <property name="driverClass"> <value>com.mysql.jdbc.Driver</value> </property> <!-- 连接数据库所用的URL --> <property name="jdbcUrl"> <value>jdbc:mysql://localhost:3306/pacs</value> </property> <!-- 连接数据库的用户名 --> <property name="user"> <value>root</value> </property> <!-- 连接数据库的密码 --> <property name="password"> <value>root</value> </property> <!-- 设置数据库连接池的最大连接数 --> <property name="maxPoolSize"> <value>20</value> </property> <!-- 设置数据库连接池的最小连接数 --> <property name="minPoolSize"> <value>2</value> </property> <!-- 设置数据库连接池的初始化连接数 --> <property name="initialPoolSize"> <value>2</value> </property> <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 --> <property name="maxIdleTime"> <value>20</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.fromat_sql">true</prop> <prop key="hibernate.generate_statistics">true</prop> <prop key="hibernate.connection.release_mode">auto</prop> <prop key="hibernate.autoReconnect">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> </props> </property> <property name="mappingResources"> <list> <value>com/d3/pacs/model/Admin.hbm.xml</value> <-- 对应model包里的映射 --> </list> </property> </bean> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="txManager" /> <-- 使用spring的注入 --> <bean id="adminDAO" class="com.d3.pacs.dao.AdminDAO"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="adminService" class="com.d3.pacs.service.AdminService"> <property name="adminDAO" ref="adminDAO" /> </bean> <bean id="adminAction" class="com.d3.pacs.action.AdminAction" scope="prototype"> <property name="adminService" ref="adminService" /> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <!-- 配置struct2--> <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> <!-- 启动Spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 默认启动项 --> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
六、编写action
public class AdminAction extends ActionSupport { private static Logger logger = Logger.getLogger(AdminAction.class); private AdminService adminService; private Admin admin; public AdminService getAdminService() { return adminService; } public void setAdminService(AdminService adminService) { this.adminService = adminService; } public Admin getAdmin() { return admin; } public void setAdmin(Admin admin) { this.admin = admin; } // 管理员登陆 public String login() { if(adminService.login(admin) !=null){ HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(true); session.setAttribute("admin1", admin); return "login"; } else return "loginFail"; } // 管理员注册 public String regist() { return "regist"; } // 退出管理员 public String exit() { HttpServletRequest request = ServletActionContext.getRequest(); request.getSession().removeAttribute("admin1"); return "exit"; } }
/my是我的tomcat自己定义的虚拟目录, admin_* 对应的是admin.xml中 <action name="admin_*" class="adminAction" method="{1}">
访问admin_login 对应的就是action类中的public String login()的方法 ,根据此方法返回的String
对应的也是admin.xml 中 ,返回login则会跳刀main.jsp
<result name="login">
/backend/main.jsp <!-- 页面跳转 -->
</result>
<result name="regist">
/backend/main.jsp <!-- 页面跳转 -->
</result>
<result name="delSuc" type="redirectAction">
<param name="actionName">admin_login</param> <!-- action跳转 -->
</result>
另外,struts会自动匹配类属性和jsp中name
比如:
jsp里面有 : <INPUT class=TxtUserNameCssClass maxLength=20 name="name">
对应在action里面,如果有name的属性,只要提供get,set方法,那么jsp中的name就会传到action
另外还可以: <INPUT class=TxtUserNameCssClass maxLength=20 name="admin.name">
对应action里面有个admin的类属性且 admin有name属性, 则会赋值
下载地址: SSH框架模板,已搭建完毕 点击下载