首页 > 代码库 > Struts2 配置

Struts2 配置

 在web.xml配置文件中(class在struts-core的jar包中)
<!-- 中央控制器 --> <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>
struts.xml验证在struts-core的jar包的struts-2.3.dtd文件中<!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd">
在struts.xml文件中修改后缀(下文配置代表后缀为 jspx和空的后缀) <constant name="struts.action.extension" value = "http://www.mamicode.com/jspx,"></constant>  
name在url中的路径,method为调用后台方法的方法名result标签中name属性为方法返回的字符串,默认为"success"<package name="mypackage" extends="struts-default">            <action name="home" class="com.peng.action.HomeAction" method="add">                <result name="success">/WEB-INF/views/home.jsp</result>            </action>                    <action name="login" class="com.peng.action.HomeAction" method="login">                <result name="success">/WEB-INF/views/home.jsp</result>            </action> </package>
在struts-core.jar包的struts-default.xml文件中定义了跳转类型。result-type标签的name,对应result的type属性。<result-types>            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>            <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>            <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>            <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>            <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>            <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />        </result-types>请求转发:chain  → 到actiondispatcher(默认) →到jsp重定向:redirect →到jspredirectAction →到action
<action name="add" class="com.peng.action.HomeAction" method="add">                <!--重定向到action  -->                <result name="success" type="redirectAction">                    <param name="actionName">login</param>                    <!-- login?code=1001 -->                    <param name="code">1001</param>                </result>                <!--请求转发到cation  -->                <result name="input" type="chain">                    <param name="actionName">login</param>                </result>            </action>