SSH框架(四) struts2+spring3.0的登陆示例
(一)关键理念及需要注意的地方:
使用struts2+spring3.0的框架搭建web程序,就是使用spring来进行依赖注入(依赖注入请参考baidu上面的解释:http://baike.baidu.com/link?url=uESWlODOsyqaaqlGLxps8xh2UaadfEe2rdsjspvZN5qsw1BOitPx_QQYuPV904jCwb493WK1ROrO3iIPZrbAQa)。
需要注意的地方如下:
- struts.xml中的action的class要写spring中的bean,这个意思就是让spring去实例化改对象。比如:<action name="Login" class="loginAction" method="execute">
- 在action的bean中在引入相关的类
<bean id="loginAction" class="com.HYOpticalComm.action.LoginAction">
<property name="loginService" ref="loginServiceImp"></property> ---这里边调用的是LoginAction类的setter函数。并使用loginServiceImp类来实例化
</bean> - 要包含“Struts 2 Spring”这个包,这个包就是struts和spring关联的包。
(二)目录结构和需要的包:
(三)各个文件相互之间的关系:
(四)代码:
<?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"> <!-- 配置FilterDispatcher过滤器,以便加Spring容器 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 指明spring配置文件在何处 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring*.xml</param-value> </context-param> <!-- 开启监听, 加载spring配置文件applicationContext.xml --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <session-config> <session-timeout>1</session-timeout> </session-config> <!-- web欢迎界面 --> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- 需要Spring时添加 --> <!-- <constant name="struts.objectFactory" value="spring" /> --> <package name="default" extends="struts-default"> <action name="Login" class="loginAction" method="execute"> <result name="success">index.jsp</result> <result name="input">login.jsp</result> </action> </package> </struts>
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"> </property> <property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=springTest"> </property> <property name="username" value="sa"></property> <property name="password" value="111111"></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.SQLServerDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/HYOpticalComm/model/Login.hbm.xml</value> </list> </property> </bean> --> <!-- <import resource="config/spring/spring-basic.xml" /> <import resource="config/spring/spring-dao.xml" /> --> <import resource="config/spring/spring-service.xml" /> <import resource="config/spring/spring-action.xml" /> </beans>
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="loginAction" class="com.HYOpticalComm.action.LoginAction"> <property name="loginService" ref="loginServiceImp"></property> </bean> </beans>
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="loginServiceImp" class="com.HYOpticalComm.serviceImp.LoginServiceImp" /> </beans>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <s:form action="Login" method="post"> <s:textfield key="username" label="用户名" /> <s:password key="password" label="密码" /> <s:submit value="登陆" /> </s:form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <div> <h4>欢迎你!</h4><font color="red">${username}</font> <br/> <h4>你登录的密码是<h4><font color="red">${password}</font>; </div> </body> </html>
package com.HYOpticalComm.action; import com.HYOpticalComm.service.ILoginService; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private String username; private String password; /* * 我们通过Spring的IOC容器注入LoginService,从而减少类之间的依赖关系 */ private ILoginService loginService; public ILoginService getLoginService() { return loginService; } public void setLoginService(ILoginService loginService) { this.loginService = loginService; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public void validate() { /* * 我们可以在这个方法类加一些输入验证 但是为了体现后面我们写的业务逻辑方法这就不验证 */ } @Override public String execute() throws Exception { boolean result = loginService.validate(username, password); if(result == true) { return SUCCESS; } else { return INPUT; } } }
package com.HYOpticalComm.service; /*接口类 */ public interface ILoginService { public boolean validate(String username,String password)throws Exception; }
package com.HYOpticalComm.serviceImp; import com.HYOpticalComm.service.ILoginService; public class LoginServiceImp implements ILoginService { /* * 我们这只是一个小的例子,不与数据库打交到 * 若有数据库操作,那么在这个LoginService就是操作具体Dao类实现登录的相关操作 */ public boolean validate(String username,String password)throws Exception { boolean v = false; if(!"admin".equals(username))//如果用户名不等于xcp,就抛出一个异常 { //throw new UsernameException("用户名不正确"); } else if(!"123".equals(password))//如果密码不等于123,就抛出一个异常 { //throw new PasswordException("密码不正确"); } else { v = true; } return v; } }