2014-08-08
学习李刚老师的j2ee整合struts2+spring3
JAR包链接
http://download.csdn.net/detail/u010393809/7732235
项目outline
1.引入JAR包,上面已经贴了JAR包下载链接
2.配置Struts2,只需要引入struts2必需的那几个包,此时暂时不要引入struts2-spring-plugin-2.2.1.jar,不然会抛出javaPointerNull的异常
配置web.xml;
配置struts.xml
<struts>
<constant name="struts.configuration.xml.reload" value="true"/>
<package name="admin" namespace="/admin" extends="struts-default" >
<action name="hello">
<result>/index.jsp</result>
</action>
</package>
</struts>
地址栏输入http://localhost:8080/SSH/admin/hello可以访问index.jsp,可知Struts2的包没有问题,能完成正常功能
下面开始整合Struts2+Spring3
1.将剩下的JAR包全部引入
2.1./content/login.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>登录页面</title> </head> <body> <h3>用户登录</h3> <s:form action="loginPro"> <s:textfield name="username" label="用户名"/><br/> <s:textfield name="password" label="密码"/><br/> <s:submit value="登录" theme="simple"/> <s:reset value="重设" theme="simple"/> </s:form> </body> </html>
2.2./content/welcome.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>成功页面</title> </head> <body> 您已经登录! <s:property value="tip"/> </body> </html>
2.3./content/error.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>错误页面</title> </head> <body> 您不能登录! </body> </html>
3.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app 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" version="3.0"> <!-- 初始化 <context-param> <param-name>contextConfigLocation</param-name> --> <!-- 如果有多个文件,在文件之间用英文逗号隔开 --> <!-- <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-db.xml </param-value> --> <!-- <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> --> <!-- 使用ContextLoaderListener初始化Spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- Struts2的常规web.xml配置 --> <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> </web-app>
4.org.crazyit.app.action/LoginAction.java
package org.crazyit.app.action; import com.opensymphony.xwork2.Action; import org.crazyit.app.service.*; public class LoginAction implements Action { //下面是用于封装用户请求参数的两个属性 private String username; private String password; //用于封装处理结果的属性 private String tip; //系统所用的业务逻辑组件 private MyService ms; //设置注入业务逻辑组件所必需的setter方法 public void setMs(MyService ms) { this.ms = ms; } //username属性的setter和getter方法 public void setUsername(String username) { this.username = username; } public String getUsername() { return this.username; } //password属性所必需的setter和getter方法 public void setPassword(String password) { this.password = password; } public String getPassword() { return this.password; } //tip属性的getter和setter方法 public void setTip(String tip) { this.tip = tip; } public String getTip() { return this.tip; } //处理用户请求的execute方法 public String execute() throws Exception { //调用业务逻辑组件的valid方法来 //验证用户输入的用户名和密码是否正确 if (ms.valid(getUsername(), getPassword())) { setTip("哈哈,整合成功!"); return SUCCESS; } else { return ERROR; } } }
5.org.crazyit.app.service.MyService.java
package org.crazyit.app.service; public interface MyService { boolean valid(String username , String pass); }
6.org.crazyit.app.service.impl.MyServiceImpl.java
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class MyServiceImpl implements MyService { public boolean valid(String username , String pass) { //此处只是简单示范,故直接判断用户名、密码 //是否符合要求 if ( username.equals("hzm") && pass.equals("123456") ) { return true; } return false; } }
7.struts.xml
<?xml version="1.0" encoding="GBK"?> <!-- 指定Struts 2配置文件的DTD信息 --> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <!-- Struts 2配置文件的根元素 --> <struts> <!-- 配置了系列常量 --> <constant name="struts.i18n.encoding" value="GBK"/> <constant name="struts.devMode" value="true"/> <package name="lee" extends="struts-default"> <!-- 定义处理用户请求的Action,该Action的class属性不是实际处理类 , 而是Spring容器中的Bean实例--> <action name="loginPro" class="loginAction"> <!-- 为两个逻辑视图配置视图页面 --> <result name="error">/content/error.jsp</result> <result name="success">/content/welcome.jsp</result> </action> <!-- 让用户直接访问该应用时列出所有视图页面 --> <action name="*"> <result>/content/{1}.jsp</result> </action> </package> </struts>
8.applicationContext.xml
<?xml version="1.0" encoding="GBK"?> <!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 --> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 定义一个业务逻辑组件,实现类为MyServiceImp --> <bean id="myService" class="org.crazyit.app.service.impl.MyServiceImpl"/> <!-- 让Spring管理的Action实例 --> <bean id="loginAction" class="org.crazyit.app.action.LoginAction" scope="prototype"> <!-- 依赖注入业务逻辑组件 --> <property name="ms" ref="myService"/> </bean> </beans>
输入http://localhost:8080/SSH/login,进入登录页面
根据逻辑判断,将跳转到/welcome.jsp或者/error.jsp
整合Struts2.2+Spring3.0,布布扣,bubuko.com