struts2的配置

    记录下struts2的配置等信息。加入的架包这里就不做记录了。

web.xml配置文件:

Web.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     id="WebApp_ID" version="2.5">
 6     <display-name>20130310</display-name>
 7 
 8     <!-- 编码转换filter -->
 9     <filter>
10         <filter-name>Set Character Encoding</filter-name>
11         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
12         <init-param>
13             <param-name>encoding</param-name>
14             <param-value>UTF-8</param-value>
15         </init-param>
16         <init-param>
17             <param-name>forceEncoding</param-name>
18             <param-value>true</param-value>
19         </init-param>
20     </filter>
21     <filter-mapping>
22         <filter-name>Set Character Encoding</filter-name>
23         <url-pattern>/*</url-pattern>
24     </filter-mapping>
25 
26     <filter>
27         <filter-name>struts2</filter-name>
28         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
29         <init-param>
30             <param-name>actionPackages</param-name>
31             <param-value>com.mycompany.myapp.actions</param-value>
32         </init-param>
33     </filter>
34     <filter-mapping>
35         <filter-name>struts2</filter-name>
36         <url-pattern>/*</url-pattern>
37     </filter-mapping>
38     <welcome-file-list>
39         <welcome-file>index.html</welcome-file>
40         <welcome-file>index.htm</welcome-file>
41         <welcome-file>index.jsp</welcome-file>
42         <welcome-file>default.html</welcome-file>
43         <welcome-file>default.htm</welcome-file>
44         <welcome-file>default.jsp</welcome-file>
45     </welcome-file-list>
46 </web-app>

struts.xml的配置:

struts.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 <struts>
 6 
 7     <!-- include节点是struts2中组件化的方式 可以将每个功能模块独立到一个xml配置文件中 然后用include节点引用 -->
 8     <include file="struts-default.xml"></include>
 9     <!-- 开启使用开发模式,详细错误提示 -->
10     <constant name="struts.devMode" value="true" />
11     <!-- 指定资源编码类型 -->
12     <constant name="struts.i18n.encoding" value="UTF-8" />
13     <!-- 指定默认locale -->
14     <constant name="struts.locale" value="zh_CN" />
15     <!-- 指定每次请求到达,重新加载资源文件 -->
16     <constant name="struts.i18n.reload" value="false" />
17     <!-- 指定每次配置文件更改后,自动重新加载 -->
18     <constant name="struts.configuration.xml.reload" value="false" />
19     <!-- 标签UI设定 -->
20     <constant name="struts.ui.theme" value="simple" />
21     <!-- 文件上传临时目录 -->
22     <constant name="struts.multipart.saveDir" value="/temp" />
23     <!-- 文件上传最大大小 -->
24     <constant name="struts.multipart.maxSize" value="2097152" />
25     <!-- 国际化资源文件 -->
26     <constant name="struts.custom.i18n.resources" value="resources/content/Language" />
27     <!-- 默认后缀名 -->
28     <constant name="struts.action.extension" value="action" />
29     <package name="mypackage" extends="struts-default">
30         <action name="firstAction" class="com.action.TestAction">
31             <result name="success">/WEB-INF/success.jsp</result>
32         </action>
33         <!-- 使用通配符 -->
34         <action name="*_*" class="com.action.{1}" method="{2}">
35             <result name="success"> /WEB-INF/success.jsp</result>
36         </action>
37     </package>
38 </struts>

TestAction2.java:

View Code
 1 package com.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class TestAction2 extends ActionSupport{
 6 
 7     /**
 8      * 
 9      */
10     private static final long serialVersionUID = 1L;
11 
12     public String tar(){
13         return "success";
14     }
15 }

test2.jsp:

test2.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>
<%response.sendRedirect("TestAction2_tar.action"); %>
</body>
</html>

success.jsp:

success.jsp
 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 I am here!
11 </body>
12 </html>

把jsp页面放在WEB-INF下,不能通过浏览器直接访问,页面重定向(redirect)也不行.所以可以通过把页面放在WEB-INF下,防止某些页面直接通过浏览器就能访问的问题.

原文地址:https://www.cnblogs.com/gexiaoshan/p/struts2.html