struts2的文件配置以及注释代码

时间:2022-03-15 12:38:33

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 此属性可用来解决乱码问题,每个版本的utf-8属性是不一样的 -->
<!-- constant主要用来设置常量配置比如加载,乱码,自定义后缀名等 -->
<!-- <constant name="struts.action.extension" value="action,,user"></constant>也可以用来设置自定义后缀名例如此句,他的配置的值可以在struts-scorejar中的org.apache.struts2.default文件中-->
<!-- 联习package和include
<include file="/user.xml"></include> 团队开发用于导入别得xml文件-->
<!-- <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" />
chain 这个结果类型代表action链,执行完actionA后执行actionB,地址栏是不变的
dispatcher 想当于servlet将后台的数据携带到需要显示的地方,默认的
<result name="success">/hi.jsp</result>

redirect 重定向action时地址栏会变,一直未懂
redirectAction 重定向到另一个action
<result name="success" type="chain">index</result>
<result name="success" type="redirectAction">index</result>
-->
<!--<global-results>定义全局结果集
<result name="success">/success.jsp</result>
</global-results>-->
<!--<default-class-ref class="com.struts2.action.Execution"></default-class-ref>
<default-action-ref name="hi"></default-action-ref>
<default-interceptor-ref ></default-interceptor-ref> 配置全局默认值 -->

<constant name="struts.i18n.encoding" value="UTF-8"></constant><!-- 乱码问题的解决 ,struts2的requestarea接口自带处理乱码问题机制-->
<package extends="struts-default" abstract="true" name="user1" namespace="/">
<interceptors>
<interceptor name="logninterceptor" class="com.struts2.action.Interceptor"></interceptor>
<interceptor-stack name="myinterceptor">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="logninterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myinterceptor"></default-interceptor-ref>

<global-results>
<result name="login">/index.jsp</result>
</global-results>
<action name="hi" class="com.struts2.action.Hiaction" method="hi">
<result name="success">/hi.jsp</result>
</action>
<action name="index" class="com.struts2.action.Execution" method="logn">
<result name="logn">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="logout" class="com.struts2.action.Execution" method="logout">
<result name="logout" type="redirect">/success.jsp</result>
</action>
</package>
</struts>

代码见文件strutsStudy