标签:
开发工具:
eclipse Neon (4.6.0)
struts2.5.2
struts2.5.2是struts当前最新版本,该版本与此前2.3版本的不同之处(包含但不仅限于):
1.struts2.5核心包已经将xwork的核心包含其中,因此在struts2版本包中的lib目录下已找不到xwork-core-xx.xx.xx.jar文件。
2.strut2.5在web.xml使用的默认过滤器已不是此前2.3版本的org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter类,而是分成了两个单独的过滤器类来配置,分别是org.apache.struts2.dispatcher.filter.StrutsPrepareFilter、org.apache.struts2.dispatcher.filter.StrutsExecuteFilter,从名称上猜测是将2.3版本中的默认过滤器拆分成了两个单独的过滤器。
struts2.5.2版本下载地址 http://mirrors.tuna.tsinghua.edu.cn/apache/struts/2.5.2/struts-2.5.2-all.zip
实例目录结构:
实例下载地址:http://pan.baidu.com/s/1slOqKNF
1.使用eclipse创建动态Web项目。
将struts2.5版本包中\apps\struts2-showcase.war包解压,将lib目录下的以下jar包复制到eclipse项目中的WebContent/WEB-INF/lib目录下:
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3.2.jar
commons-io-2.4.jar
commons-lang3-3.4.jar
commons-logging-1.1.3.jar
freemarker-2.3.23.jar
javassist-3.20.0-GA.jar
log4j-api-2.5.jar
ognl-3.1.10.jar
struts2-core-2.5.2.jar
2.在WEB-INF目录下创建web.xml文件(若创建项目时自动创建则不再需要重新创建),修改web.xml配置内容如下(参照struts2-showcase.war)的配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <display-name>mystruts2</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <filter> 13 <filter-name>struts-prepare</filter-name> 14 <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class> 15 </filter> 16 17 <filter> 18 <filter-name>struts-execute</filter-name> 19 <filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class> 20 </filter> 21 <filter-mapping> 22 <filter-name>struts-prepare</filter-name> 23 <url-pattern>/*</url-pattern> 24 </filter-mapping> 25 26 <filter-mapping> 27 <filter-name>struts-execute</filter-name> 28 <url-pattern>/*</url-pattern> 29 </filter-mapping> 30 </web-app>
3.在src目录下创建struts.xml,文件内容可参照struts版本包自带的struts2-showcase.war实例中的web.xml。
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 5 "http://struts.apache.org/dtds/struts-2.5.dtd"> 6 7 <!-- START SNIPPET: xworkSample --> 8 <struts> 9 10 <package name="wei" extends="struts-default"> 11 12 <action name="login" class="com.info.action.LoginAction" method="execute"> 13 <result name="success">/WEB-INF/welcome.jsp</result> 14 <result name="error">/WEB-INF/error.jsp</result> 15 </action> 16 17 </package> 18 19 20 </struts> 21 22 <!-- END SNIPPET: xworkSample -->
4.创建jsp页面。
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags" %> 4 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <s:form action="login" method="post"> 13 <s:textfield name="username" label="用户名"></s:textfield> 14 <s:password name="password" label="密码"></s:password> 15 16 <s:submit value="提交"></s:submit> 17 </s:form> 18 </body> 19 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!-- <%@taglib prefix="s" uri="/struts-tags" %> --> 4 <%@ taglib prefix="s" uri="/struts-tags" %> 5 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 10 <title>welcome</title> 11 </head> 12 <body> 13 welcome! 14 </body> 15 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!-- <%@taglib prefix="s" uri="/struts-tags" %> --> 4 <%@ taglib prefix="s" uri="/struts-tags" %> 5 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 10 <title>error</title> 11 </head> 12 <body> 13 error! 14 </body> 15 </html>
5.src目录下创建action类。
1 package com.info.action; 2 3 public class LoginAction { 4 5 private String username; 6 private String password; 7 8 public String getUsername() { 9 return username; 10 } 11 12 public void setUsername(String username) { 13 this.username = username; 14 } 15 16 public String getPassword() { 17 return password; 18 } 19 20 public void setPassword(String password) { 21 this.password = password; 22 } 23 24 public String execute() throws Exception { 25 26 if(this.getUsername().equals("admin")&&this.getPassword().equals("admin")) 27 { 28 return "success"; 29 } 30 31 return "error"; 32 33 } 34 35 }
标签:
原文地址:http://www.cnblogs.com/labing/p/5883497.html