(一) struts2 与struts1 区别     

    在Action的实现方面:Struts1要求必须统一扩展自Action类,而Struts2中可以是一个普通的POJO。

  线程模型方面:Struts1的Action工作在单例模式,一个Action的实例处理所有的请求。Struts2的Action是一个请求对应一个实例。没有线程安全方面的问题。

  Servlet依赖方面:Struts1的Action依赖于Servlet API,比如Action的execute方法的参数就包括request和response对象。这使程序难于测试。Struts2中的Action不再依赖于Servlet API,有利于测试,并且实现TDD。

  封装请求参数:Struts1中强制使用ActionForm对象封装请求的参数。Struts2可以选择使用POJO类来封装请求的参数,或者直接使用Action的属性。

  表达式语言方面:Struts1中整合了EL,但是EL对集合和索引的支持不强,Struts2整合了OGNL(Object Graph NavigationLanguage)。

  绑定值到视图技术:Struts1使用标准的JSP,Struts2使用“ValueStack”技术。

  类型转换:Struts1中的ActionForm基本使用String类型的属性。Struts2中使用OGNL进行转换,可以更方便的使用。

  数据校验:Struts1中支持覆盖validate方法或者使用Validator框架。Struts2支持重写validate方法或者使用XWork的验证框架。

  Action执行控制的对比:Struts1支持每一个模块对应一个请求处理,但是模块中的所有Action必须共享相同的生命周期。Struts2支持通过拦截器堆栈为每一个Action创建不同的生命周期。以上是引用百度百科。

(二) 搭建struts2 必须的包

commons-fileupload-1.2.2.jar    文件上传组件,2.1.6版本后必须加入此jar包

commons-io-2.0.1.jar 日志包

commons-lang-2.5.jar

commons-logging-1.1.1.jar

freemarker-2.3.18.jar   struts2的UI标签的模板使用freemarker编写

javassist-3.11.0.GA.jar

ognl-3.0.4.jar  对象图导航语言,通过它来读写对象属性

struts2-core-2.3.1.2.jar  开发的核心类库

xwork-core-2.3.1.2.jar  xwork类库,struts2在其上进行构建

(三)struts2配置文件

(1).web.xml文件
主要完成对StrutsPrepareAndExecuteFilter的配置(在以前的版本中是对FilterDispatcher配置,新版本同样支持用FilterDispatcher配置),它的实质是一个过滤器,它负责初始化整个Struts框架并且处理所有的请求。这个过滤器可以包括一些初始化参数,有的参数指定了要加载哪些额外的xml配置文件,还有的会影响struts框架的行为。除了StrutsPrepareAndExecuteFilter外,Struts还提供了一个ActionContexCleanUp类,它的主要任务是当有其它一些过滤器要访问一个初始化好了的struts框架的时候,负责处理一些特殊的清除任务。
(2).struts.xml文件
框架的核心配置文件就是这个默认的struts.xml文件,在这个默认的配置文件里面我们可以根据需要再包括其它一些配置文件。在通常的应用开发中,我们可能想为每个不同的模块单独配置一个struts.xml文件,这样也利于管理和维护。这也是我们要配置的主要文件。
(3).struts.properties(参default.properties)
在Struts框架使用了很多属性,我们可以通过改变这些属性来满足我们的需求。要改变这些属性,只需在struts.properties文件中指定属性的key和value即可。属性文件可以放在任何一个包含在classpath中的路径上,但是通常我们都把它放在/WEB-INF/classes目录下面。我们可以在struts-default.properties文件中找到一个属性的列表。

例子 :

struts.xml:

<?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="test" namespace="/test" extends="struts-default">
  <action name="login" class="com.manyu.action.LoginAction">
   <result name="success">/success.jsp</result>
   <result name="fail">/fail.jsp</result>
  </action>
 </package>
</struts>
web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <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>

 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

LoginAction:

package com.manyu.action;

public class LoginAction {
 private String userName; // 必须严格命名,不能使userName 不然不能获取获取表单数据
 private String password;

 public String getPassword() {
  return password;
 }

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String execute() throws Exception {
  System.out.println("222");
  System.out.println(userName);
  if (userName.equals("struts2")) {
   return "success";
  } else {
   return "fail";
  }

 }
}
 

index.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="login" namespace="/test">
<s:textfield label="用户名" name="userName" > <!-- 必须有name 与action的对应  不能是 id-->
</s:textfield>
<s:textfield label="密码" name="password">
</s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>

fail.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 fail
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
success
</body>
</html>