1、 pom.xml文件添struts2依赖jar包;
<!-- 与Struts2集成必须使用 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- Struts2的核心包 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.16</version> </dependency> <!-- Struts2和Spring整合插件 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.4.1</version> </dependency>
2、 web.xml文件添加struts2拦截器;
<!-- Struts2的核心过滤器配置 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- Struts2过滤器拦截所有的.action请求 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>
3、 src/main/resources目录下添加struts.xml文件;
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 指定由spring负责action对象的创建 --> <constant name="struts.objectFactory" value="spring"/> <!-- 所有匹配*.action的请求都由struts2处理 --> <constant name="struts.action.extension" value="action"/> <!-- 是否启用开发模式(开发时设置为true,发布到生产环境后设置为false) --> <constant name="struts.devMode" value="true" /> <!-- struts配置文件改动后,是否重新加载(开发时设置为true,发布到生产环境后设置为false) --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 设置浏览器是否缓存静态内容(开发时设置为false,发布到生产环境后设置为true) --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 请求参数的编码方式 --> <constant name="struts.i18n.encoding" value="utf-8" /> <!-- 每次HTTP请求系统都重新加载资源文件,有助于开发(开发时设置为true,发布到生产环境后设置为false) --> <constant name="struts.i18n.reload" value="true" /> <!-- 文件上传最大值 --> <constant name="struts.multipart.maxSize" value="104857600" /> <!-- 让struts2支持动态方法调用,使用叹号访问方法 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- Action名称中是否还是用斜线 --> <constant name="struts.enable.SlashesInActionNames" value="false" /> <!-- 允许标签中使用表达式语法 --> <constant name="struts.tag.altSyntax" value="true" /> <!-- 对于WebLogic,Orion,OC4J此属性应该设置成true --> <constant name="struts.dispatcher.parametersWorkaround" value="false" /> <include file="strutsLogin.xml"/> <package name="basePackage" extends="struts-default"> </package> </struts>
4、 添加Action测试类;
package com.coshaho.learn.service.action; import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Controller; @Controller("login") public class LoginAction { private static Map<String, String> userMap = new HashMap<String, String>(); static { userMap.put("hekexu", "hekexu"); userMap.put("zhubing", "zhubing"); } private String username; private String password; 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; } public String authority() { String curpassword = userMap.get(username); if(curpassword != null && curpassword.equals(password)) { return "success"; } return "error"; } }
5、 src/main/resources目录下添加strutsLogin.xml文件;
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="framework" extends="struts-default" namespace="/framework"> <action name="login" class="login"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> <action name="authority" class="login" method="authority"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
6、 添加登陆jsp文件;
login.jsp
<html> <head> <%String path = request.getContextPath();%> </head> <body> <form action="<%=path%>/framework/authority.action" method="post"> UserName:<input type="text" name="username"/><p/> Password:<input type="password" name="password"/><p/> <input type="submit" name="submit"/> </form> </body> </html>
success.jsp
<html> <head> </head> <body> <h2>Login successfully!</h2> </body> </html>
error.jsp
<html> <head> </head> <body> <h2>Login failure!</h2> </body> </html>
7、 发布,访问
Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(二)
原文:http://www.cnblogs.com/coshaho/p/5060267.html