最近对Struts2做了一些小总结,也不算全面,总的来说也算还行;关于Struts2还有很多的知识点,我也没有完全掌握,目前也就把我会的一些给帖出来,也还请大家多多指点

项目结构:

Struts2 ---- 一些小总结_struts




web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<!-- 配置Struts2 的核心过虐器 ; (FilterDispatcher是2.1.2之前的核心过滤器;从2.1.3版本始,就用StrutsPrepareAndExecuteFilter代替FilterDispatcher) -->
<!-- StrutsPrepareAndExecuteFilter :过滤用户请求、初始化请求处理环境、创建Action,调用Action的execute方法、根据execute方法返回结果,将请求转发给相应的结果页面 -->
<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>
<!-- 拦截所有,我会在struts.xml中配置静态资源的释放 -->
<url-pattern>/*</url-pattern>
</filter-mapping>



</web-app>



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>
<!-- 配置静态资源的释放 -->
<constant name="struts.action.excludePattern" value="/static/.*?"/>

<!--<!&ndash;指定Web应用的默认编码集.该属性对于处理中文请求参数非常有用,对于获取中文请求参数值,应该将该属性值设置为GBK或者GB2312;-->
<!--提示:当设置该参数为GBK时,相当于调用HttpServletRequest的setCharacterEncoding方法. &ndash;>-->
<!--<constant name="struts.i18n.encoding" value="UTF-8"/>-->

<!--<!&ndash; 指定Struts 2默认的ObjectFactory Bean,该属性默认值是spring. &ndash;>-->
<!--<constant name="struts.objectFactory" value="spring"/>-->

<!--<!&ndash; 指定Spring框架的自动装配模式, 该属性的默认值是name, 即默认根据Bean的name属性自动装配. &ndash;>-->
<!--<constant name="struts.objectFactory.spring.autoWire" value="name"/>-->

<!--<!&ndash; 该属性指定整合Spring框架时,是否缓存Bean实例,该属性只允许使用true和false两个属性值,它的默认值是true.通常不建议修改该属性值. &ndash;>-->
<!--<constant name="struts.objectFactory.spring.useClassCache" value="true"/>-->

<!--<!&ndash; 该属性指定处理multipart/form-data的MIME类型(文件上传)请求的框架,该属性支持cos,pell和jakarta等属性值, 即分别对应使用cos的文件上传框架,pell上传及common-fileupload文件上传框架.该属性的默认值为jakarta.-->
<!--注意:如果需要使用cos或者pell的文件上传方式,则应该将对应的JAR文件复制到Web应用中.例如,使用cos上传方式,则需要自己下载cos框架的JAR文件,并将该文件放在WEB-INF/lib路径下. &ndash;>-->
<!--<constant name="struts.multipart.parser" value="jakarta"/>-->

<!--<!&ndash; 该属性指定上传文件的临时保存路径,该属性的默认值是javax.servlet.context.tempdir. &ndash;>-->
<!--<constant name="struts.multipart.saveDir" value=""/>-->

<!--<!&ndash; 该属性指定Struts 2文件上传中整个请求内容允许的最大字节数. &ndash;>-->
<!--<constant name="struts.multipart.maxSize" value="1000000000"/>-->

<!--<!&ndash; 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理.如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开. &ndash;>-->
<!--<constant name="struts.action.extension" value="do"/>-->

<!--<!&ndash; 该属性设置是否通过JAR文件提供静态内容服务,该属性只支持true和false属性值,该属性的默认属性值是true. &ndash;>-->
<!--<constant name="struts.serve.static" value="true"/>-->

<!--<!&ndash; 该属性设置浏览器是否缓存静态内容.当应用处于开发阶段时,我们希望每次请求都获得服务器的最新响应,则可设置该属性为false. &ndash;>-->
<!--<constant name="struts.serve.static.browserCache" value="true"/>-->

<!--<!&ndash; 该属性设置Struts 2应用是否使用开发模式.如果设置该属性为true,则可以在应用出错时显示更多、更友好的出错提示.该属性只接受true和flase两个值,该属性的默认值是false.通常,应用在开发阶段,将该属性设置为true,当进入产品发布阶段后,则该属性设置为false. &ndash;>-->
<!--<constant name="struts.devMode" value="false"/>-->

<!--<!&ndash; 该属性设置是否每次HTTP请求到达时,系统都重新加载资源文件(允许国际化文件重载).该属性默认值是false.在开发阶段将该属性设置为true会更有利于开发,但在产品发布阶段应将该属性设置为false.-->
<!--提示:开发阶段将该属性设置了true,将可以在每次请求时都重新加载国际化资源文件,从而可以让开发者看到实时开发效果;产品发布阶段应该将该属性设置为false,是为了提供响应性能,每次请求都需要重新加载资源文件会大大降低应用的性能. &ndash;>-->
<!--<constant name="struts.i18n.reload" value="false"/>-->

<!--<!&ndash; 该属性指定视图标签默认的视图主题,该属性的默认值是xhtml. &ndash;>-->
<!--<constant name="struts.ui.theme" value="simple"/>-->

<!--<!&ndash; 该属性指定模板文件的后缀,该属性的默认属性值是ftl.该属性还允许使用ftl、vm或jsp,分别对应FreeMarker、Velocity和JSP模板. &ndash;>-->
<!--<constant name="struts.ui.templateSuffix" value="ftl"/>-->

<!--<!&ndash; 该属性设置当struts.xml文件改变后,系统是否自动重新加载该文件.该属性的默认值是false. &ndash;>-->
<!--<constant name="struts.configuration.xml.reload" value="false"/>-->

<!--<!&ndash; 该属性指定Struts 2应用所需要的国际化资源文件,如果有多份国际化资源文件,则多个资源文件的文件名以英文逗号(,)隔开. &ndash;>-->
<!--<constant name="struts.custom.i18n.resources" value="nationz"/>-->

<!--<!&ndash; 对于某些Java EE服务器,不支持HttpServlet Request调用getParameterMap()方法,此时可以设置该属性值为true来解决该问题.该属性的默认值是false.对于WebLogic、Orion和OC4J服务器,通常应该设置该属性为true. &ndash;>-->
<!--<constant name="struts.dispatcher.parametersWorkaround" value="false"/>-->

<!--<!&ndash; 指定是否缓存FreeMarker模版。默认值false。&ndash;>-->
<!--<constant name="struts.freemarker.templatesCache" value="true"/>-->

<!--<!&ndash; 该属性只支持true和false两个属性值,默认值是true.通常无需修改该属性值. &ndash;>-->
<!--<constant name="struts.freemarker.wrapper.altMap" value="true"/>-->

<!--<!&ndash; 该属性指定XSLT Result是否使用样式表缓存.当应用处于开发阶段时,该属性通常被设置为true;当应用处于产品使用阶段时,该属性通常被设置为false. &ndash;>-->
<!--<constant name="struts.xslt.nocache" value="false"/>-->

<!--<!&ndash; 该属性指定Struts 2框架默认加载的配置文件,如果需要指定默认加载多个配置文件,则多个配置文件的文件名之间以英文逗号(,)隔开.该属性的默认值为struts-default.xml,struts-plugin.xml,struts.xml,看到该属性值,所以应该明白为什么Struts 2框架默认加载struts.xml文件了. &ndash;>-->
<!--<constant name="struts.configuration.files" value="struts-default.xml,struts-plugin.xml"/>-->

<!--<!&ndash; 设置映射器是否总是选择完整的名称空间。该属性的默认值时false。&ndash;>-->
<!--<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>-->

<!--<!&ndash; 设置Convention插件定位视图资源的根路径。默认值为/WEB-INF/content &ndash;>-->
<!--<constant name="struts.convention.result.path" value="/WEB-INF/content/"/>-->

<!--<!&ndash; Convention插件以该常量指定包作为根包 &ndash;>-->
<!--<constant name="struts.convention.action.package" value="default"/>-->

<!--<!&ndash; 是否从包中搜索Action &ndash;>-->
<!--<constant name="struts.convention.action.disableScanning" value="false"/>-->

<!--<!&ndash; 官方只说明在jboss下需要设置,情况不明&ndash;>-->
<!--<constant name="struts.convention.exclude.parentClassLoader" value="true"/>-->
<!--<constant name="struts.convention.action.fileProtocols" value="jar,zip"/>-->

<!--<!&ndash; 包括哪些jar包中的action。逗号分割字符串。&ndash;>-->
<!--<constant name="struts.convention.action.includeJars" value=".*?/_wl_cls_gen.*?jar(!/)?"/>-->

<!--<!&ndash; 确定搜索包的路径。只要是结尾为action的包都要搜索。&ndash;>-->
<!--<constant name="struts.convention.package.locators" value="action"/>-->

<!--<!&ndash; 是否支持动态方法调用,url名称为 Action的name值!方法名.action &ndash;>-->
<!--<constant name="struts.enable.DynamicMethodInvocation" value="false"/>-->
</struts>



TestAction.java

package action;

import com.opensymphony.xwork2.ActionSupport;
import entity.Company;
import org.apache.struts2.convention.annotation.*;

/**
* @author Ming
* @date 2017/12/22 16:14
*/

/**
* ActionSupport这个工具类在实现了Action的基础方法上还定义了很多方法,如数据校验、国际化支持等。
* @ParentPackage:指定action所处的父包名,一般为struts-default(action的结果类型为json; 那么就需要继承json-default包@ParentPackage("json-default"))
* @Namespace:URL命名空间 localhost:8080/TestStruts/process.action;列:(@Namespace("/test") :localhost:8080/TestStruts/test/process.action )
* @ResultPath:结果视图基路径 @ResultPath("/WEB-INF/content/") 那么我们在@Result(location = "ok.jsp"),访问的页面就将是/WEB-INF/content/ok.jsp ;@Result(location = "/ok.jsp")加斜杠的话@ResultPath将会无效,则可以不写
*/
@ParentPackage("struts-default")
@Namespace("/")
@ResultPath("/WEB-INF/content/")
public class TestAction extends ActionSupport {
// 为其提供get set 方法 ;其余类型也大致一样,这里只是举例
private String username;
private Company company;

/**
* SUCCESS、LOGIN、INPUT、ERROR、NONE 只是5个标准返回值 , 当然我们可以自己随意定义返回的名字;
* @Action:指定访问URL value : url results : Result注解列表
* @Result : 提供了Action结果的映射。(一个结果的映射)
* @Result(name = "login" ,type = "dispatcher", location = "no.jsp") : name : 匹配返回值 type: dispatcher(转发),redirect(重定向),chain(action链),redirectAction(重定向到一个action) ;location:映射页面
* params传参 :
*/
@Action(value = "process", results = {
@Result(location = "ok.jsp"),
@Result(name = "login" ,type = "dispatcher", location = "no.jsp")
})
public String execute(){
if (username.equals("小明") && company.getDeptName().equals("销售部")){
System.out.println(username);
System.out.println(company.getDeptName());
return SUCCESS;
}
System.out.println(username);
System.out.println(company.getDeptName());
return LOGIN;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public Company getCompany() {
return company;
}

public void setCompany(Company company) {
this.company = company;
}
}



Company.java

package entity;

/**
* @author Ming
* @date 2017/12/22 16:35
* POJO
*/
public class Company {
private int deptId;
private String deptName;
private int empId;
private String empName;

public int getDeptId() {
return deptId;
}

public void setDeptId(int deptId) {
this.deptId = deptId;
}

public String getDeptName() {
return deptName;
}

public void setDeptName(String deptName) {
this.deptName = deptName;
}

public int getEmpId() {
return empId;
}

public void setEmpId(int empId) {
this.empId = empId;
}

public String getEmpName() {
return empName;
}

public void setEmpName(String empName) {
this.empName = empName;
}
}



index.jsp

<%--
Created by IntelliJ IDEA.
User: Ming
Date: 2017/12/22
Time: 15:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
<head>
<base href="<%=basePath%>">
<title>$Title$</title>
</head>
<body>
<form action="process.action" method="post">
姓名:<input type="text" name="username" value="${username}"><br>
部门:<input type="text" name="company.deptName" value="${company.deptName}"><br>
<input type="submit" value="提交">
</form>
</body>
</html>



ok.jsp

<%--
Created by IntelliJ IDEA.
User: Ming
Date: 2017/12/22
Time: 17:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<html>
<head>
<base href="<%=basePath%>">
<title>OK页面</title>
</head>
<body>
姓名:<input type="text" name="username" value="${username}"><br>
部门:<input type="text" name="company.deptName" value="${company.deptName}"><br>
</body>
</html>



no.jsp

<%--
Created by IntelliJ IDEA.
User: Ming
Date: 2017/12/22
Time: 17:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<html>
<head>
<base href="<%=basePath%>">
<title>NO页面</title>
</head>
<body>
姓名:<input type="text" name="username" value="${username}"><br>
部门:<input type="text" name="company.deptName" value="${company.deptName}"><br>
</body>
</html>