一、Struts2简介
Struts2以WebWork优秀的设计思想为核心,吸收了Struts1的部分优点,建立了一个基于WebWork和Struts1的MVC框架。
二、搭建Struts2开发环境
2.1、通过官网下载最新版:http://struts.apache.org/download.cgi
建议下载struts-xx.all.zip包,压缩包中不仅包含struts2的jar还包含了示例、源码以及帮助文档。
2.2、在项目中导入Struts2需要的jar包
2.3、修改web.xml文件
在web.xml文件的<web-app>节点下添加StrutsPrepareAndExecuteFilter核心过滤器,主要负责拦截用户的请求,交给Struts2的框架进行处理。
2.4、添加struts.xml配置文件。
struts.xml是Struts2的核心配置文件,该文件通常放在src目录下,编译部署以后,他就到了应用程序的WEB-INF\classes目录下。
三、使用struts2输出Hello World
3.1、新建web项目,并导入struts的jar包
3.2、添加Action类
实现Action可以有三种方法:
1.使用普通的Java类,编写public String execute()方法
2.实现Action接口,实现execute()方法
3.继承ActionSupport类,重写execute()方法。
package com.lxx.action; import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport { private String name;
private String message; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} /**
* 这个方法名可以随便写
*
* @return
*/
public String execute() {
System.out.println(name + "-----------------");
message = "Hello World " + name;
return "abc";
}
}
3.3、修改web.xml
<?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"> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- 加载spring 配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/applicationContext.xml</param-value>
</context-param> <!-- 使用ContextLoaderListener初始化Spring容器 -->
<!--若没有指定其他参数,默认查找的配置文件位置是:/WEB-INF/applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 定义Struts 2的FilterDispathcer的Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- FilterDispatcher用来初始化Struts 2并且处理所有的WEB请求。 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.4、在src目录下添加struts.xml,并添加相应的配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定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>
<!-- 配置了系列常量 -->
<!-- struts.i18n.encoding确实用于了HttpServletRequest的setCharacterEncoding()方法 -->
<constant name="struts.i18n.encoding" value="utf-8"/>
<constant name="struts.devMode" value="true"/>
<!-- 与spring集成 -->
<constant name="struts.objectFactory" value="spring"/> <!-- 配置默认Action -->
<package name= "default" extends= "struts-default" >
<default-action-ref name= "defaultAction" ></default-action-ref>
<action name= "defaultAction" >
<result>/error.jsp </result>
</action>
</package> <package name="lxx" extends="struts-default">
<!-- 执行的是代理类 -->
<action name="testAction" class="com.lxx.action.TestAction" method="execute">
<result name="abc" >index.jsp</result> <!--type="forword"-->
</action>
</package>
</struts>
3.5、修改index.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<br>
<form action="testAction">
用户名: <input name="name"/><br/><br/>
<input type="submit" value="提交"/>
</form>
<div>
<s:property value="message"/>
</div>
</body>
</html>
3.6、将项目添加到tomcat中启动项目,在浏览器中输入:localhost:8080/S2SI(自己的项目名)/testAction就可以看到页面跳转到index.jsp页面,然后在文本框中输入lixiaoxi提交后,在下边输出message。
四、总结
4.1、在浏览器中请求testAction时会经过Struts的核心过滤器“StrutsPrepareAndExecuteFilter”。
4.2、核心过滤器会根据请求在struts.xml中匹配name相同的action,然后会跳转到相应action处理类TestAction类。
4.3、会在TestAction寻找与struts.xml中配置的<action method=“exectue”>标签中属性method相同值的方法,默认执行行方法名是execute()方法,如果在标签中定义method="add",就会在TestAction类中寻求add的方法。
4.4、执行完execute()方法后根据返回值String,寻找匹配struts.xml中<action></action>标签中的<resutl>值相同的result。
4.5、根据类型跳转相应的请求中,上示例定义跳转到index.jsp中,所以会在页面中看到TsetAction类中execute方法中返回的message值“Hello world ”+name,name是页面提交表单中的值。