三大框架(ssh)学习——配置文件详解(一)

配置文件详解

指定web应用默认字符集

<constant name="struts.i18n.encoding" value="gbk" />

此配置相当于:

request.setCharacterEncoding(gbk);

response.setCharacterEncoding(gbk);

开发模式的使用

开发阶段,我们一般将指定开发模式,这样我们修改action、修改xml文件后不需要再重启服务器,直接就可以看到结果。 但是,项目上线后,需要关闭开发模式。

<constant name="struts.devMode" value="true" />

package配置

此处package(包)不是指java中的包。而是struts配置文件中的<package>元素。

package就是把action、result、type、interceptor等这些元素打包到一个逻辑单元中。从而便于统一管理。package可以被其他包继承,可以拥有自己独立的部分,包中内容可以被子包重写。

    <package name="default" namespace="/" extends="struts-default">

        <action name="first" class="com.bjsxt.struts.test.FirstAction">

           <result name="success">aa.txt</result>

        </action>

    </package>

我们定义的包,继承了struts-default包。这个包位于:struts-default.xml中。他的内容如下:

  <package name="struts-default" abstract="true">

        <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>

 

        <interceptors>

            <interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>

            <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>

            <interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>

            <interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>

            <interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>

            <interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" />

            <interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />

            <interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />

            <interceptor name="externalRef" class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>

            <interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>

            <interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>

            <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>

            <interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>

            <interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>

            <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>

            <interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>

            <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

            <interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>

            <interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>

            <interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>

            <interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>

            <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>

            <interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>

            <interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>

            <interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>

            <interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>

            <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>

            <interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>

            <interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />

            <interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />

            <interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />

            <interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />

            <interceptor name="jsonValidation" class="org.apache.struts2.interceptor.validation.JSONValidationInterceptor" />

            <interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />

            <interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" />

 

            <!-- Basic stack -->

            <interceptor-stack name="basicStack">

                <interceptor-ref name="exception"/>

                <interceptor-ref name="servletConfig"/>

                <interceptor-ref name="prepare"/>

                <interceptor-ref name="checkbox"/>

                <interceptor-ref name="multiselect"/>

                <interceptor-ref name="actionMappingParams"/>

                <interceptor-ref name="params">

                    <param name="excludeParams">dojo\..*,^struts\..*</param>

                </interceptor-ref>

                <interceptor-ref name="conversionError"/>

            </interceptor-stack>

 

            <!-- Sample validation and workflow stack -->

            <interceptor-stack name="validationWorkflowStack">

                <interceptor-ref name="basicStack"/>

                <interceptor-ref name="validation"/>

                <interceptor-ref name="workflow"/>

            </interceptor-stack>

 

            <!-- Sample JSON validation stack -->

            <interceptor-stack name="jsonValidationWorkflowStack">

                <interceptor-ref name="basicStack"/>

                <interceptor-ref name="validation">

                    <param name="excludeMethods">input,back,cancel</param>

                </interceptor-ref>

                <interceptor-ref name="jsonValidation"/>

                <interceptor-ref name="workflow"/>

            </interceptor-stack>

 

            <!-- Sample file upload stack -->

            <interceptor-stack name="fileUploadStack">

                <interceptor-ref name="fileUpload"/>

                <interceptor-ref name="basicStack"/>

            </interceptor-stack>

 

            <!-- Sample model-driven stack  -->

            <interceptor-stack name="modelDrivenStack">

                <interceptor-ref name="modelDriven"/>

                <interceptor-ref name="basicStack"/>

            </interceptor-stack>

 

            <!-- Sample action chaining stack -->

            <interceptor-stack name="chainStack">

                <interceptor-ref name="chain"/>

                <interceptor-ref name="basicStack"/>

            </interceptor-stack>

 

            <!-- Sample i18n stack -->

            <interceptor-stack name="i18nStack">

                <interceptor-ref name="i18n"/>

                <interceptor-ref name="basicStack"/>

            </interceptor-stack>

 

            <!-- An example of the paramsPrepareParams trick. This stack

                 is exactly the same as the defaultStack, except that it

                 includes one extra interceptor before the prepare interceptor:

                 the params interceptor.

 

                 This is useful for when you wish to apply parameters directly

                 to an object that you wish to load externally (such as a DAO

                 or database or service layer), but can't load that object

                 until at least the ID parameter has been loaded. By loading

                 the parameters twice, you can retrieve the object in the

                 prepare() method, allowing the second params interceptor to

                 apply the values on the object. -->

            <interceptor-stack name="paramsPrepareParamsStack">

                <interceptor-ref name="exception"/>

                <interceptor-ref name="alias"/>

                <interceptor-ref name="i18n"/>

                <interceptor-ref name="checkbox"/>

                <interceptor-ref name="multiselect"/>

                <interceptor-ref name="params">

                    <param name="excludeParams">dojo\..*,^struts\..*</param>

                </interceptor-ref>

                <interceptor-ref name="servletConfig"/>

                <interceptor-ref name="prepare"/>

                <interceptor-ref name="chain"/>

                <interceptor-ref name="modelDriven"/>

                <interceptor-ref name="fileUpload"/>

                <interceptor-ref name="staticParams"/>

                <interceptor-ref name="actionMappingParams"/>

                <interceptor-ref name="params">

                    <param name="excludeParams">dojo\..*,^struts\..*</param>

                </interceptor-ref>

                <interceptor-ref name="conversionError"/>

                <interceptor-ref name="validation">

                    <param name="excludeMethods">input,back,cancel,browse</param>

                </interceptor-ref>

                <interceptor-ref name="workflow">

                    <param name="excludeMethods">input,back,cancel,browse</param>

                </interceptor-ref>

            </interceptor-stack>

 

            <!-- A complete stack with all the common interceptors in place.

                 Generally, this stack should be the one you use, though it

                 may do more than you need. Also, the ordering can be

                 switched around (ex: if you wish to have your servlet-related

                 objects applied before prepare() is called, you'd need to move

                 servletConfig interceptor up.

 

                 This stack also excludes from the normal validation and workflow

                 the method names input, back, and cancel. These typically are

                 associated with requests that should not be validated.

                 -->

            <interceptor-stack name="defaultStack">

                <interceptor-ref name="exception"/>

                <interceptor-ref name="alias"/>

                <interceptor-ref name="servletConfig"/>

                <interceptor-ref name="i18n"/>

                <interceptor-ref name="prepare"/>

                <interceptor-ref name="chain"/>

                <interceptor-ref name="debugging"/>

                <interceptor-ref name="scopedModelDriven"/>

                <interceptor-ref name="modelDriven"/>

                <interceptor-ref name="fileUpload"/>

                <interceptor-ref name="checkbox"/>

                <interceptor-ref name="multiselect"/>

                <interceptor-ref name="staticParams"/>

                <interceptor-ref name="actionMappingParams"/>

                <interceptor-ref name="params">

                  <param name="excludeParams">dojo\..*,^struts\..*</param>

                </interceptor-ref>

                <interceptor-ref name="conversionError"/>

                <interceptor-ref name="validation">

                    <param name="excludeMethods">input,back,cancel,browse</param>

                </interceptor-ref>

                <interceptor-ref name="workflow">

                    <param name="excludeMethods">input,back,cancel,browse</param>

                </interceptor-ref>

            </interceptor-stack>

 

            <!-- The completeStack is here for backwards compatibility for

                 applications that still refer to the defaultStack by the

                 old name -->

            <interceptor-stack name="completeStack">

                <interceptor-ref name="defaultStack"/>

            </interceptor-stack>

 

            <!-- Sample execute and wait stack.

                 Note: execAndWait should always be the *last* interceptor. -->

            <interceptor-stack name="executeAndWaitStack">

                <interceptor-ref name="execAndWait">

                    <param name="excludeMethods">input,back,cancel</param>

                </interceptor-ref>

                <interceptor-ref name="defaultStack"/>

                <interceptor-ref name="execAndWait">

                    <param name="excludeMethods">input,back,cancel</param>

                </interceptor-ref>

            </interceptor-stack>

       </interceptors>

        <default-interceptor-ref name="defaultStack"/>

        <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />

    </package>

显然,struts-default包中定义的内容,由于我们继承了他,所以可以直接使用。我们自定义的包一般都需要继承struts-default包或者他的子包。

配置命名空间(namespace)

我们可以通过不同的命名空间划分不同的功能模块,这样,即使Action的名字相同只要位于不同的命名空间和不同的保重中也可以正常执行。有利于多人协作开发时保持相互之间的独立性,从而提高开发效率。

    <package name="test" namespace="/ceshi" extends="struts-default">

        <action name="first" class="com.bjsxt.struts.test.FirstAction">

           <result name="success">/ok.jsp</result>

        </action>

    </package>

我们在地址栏输入:http://localhost/teststruts/ceshi/first!test2   即可正常执行!

配置Action

Action的配置信息是将:url、Action类、展示页面。三者联系起来。

url跟action的name属性对应。

class属性指定了Action类

<result>元素将Action中返回的字符串(通过result的name属性匹配)跟展示页面(通过path或者result元素的文本值指定)映射起来。

        <action name="first" class="com.bjsxt.struts.test.FirstAction">

           <result name="success">/ok.jsp</result> 

<!可以有多个result元素 -->

        </action>

配置Result

<result>元素告诉了我们调用完Action后下一步做什么。<result>一般作为<action>的子元素,或者我们可以通过<global-result>定义全局result。

<result>有三个基本内容:

1. name   (跟action返回的字符串匹配,从而根据不同的字符串匹配不同的result)

name如果不指定值,默认是”success”。

Action接口中定义了多个常量,方便我们统一指定name。

    /**

     * The action execution was successful. Show result

     * view to the end user.

     */

    public static final String SUCCESS = "success";

 

    /**

     * The action execution was successful but do not

     * show a view. This is useful for actions that are

     * handling the view in another fashion like redirect.

     */

    public static final String NONE = "none";

 

    /**

     * The action execution was a failure.

     * Show an error view, possibly asking the

     * user to retry entering data.

     */

    public static final String ERROR = "error";

 

    /**

     * The action execution require more input

     * in order to succeed.

     * This result is typically used if a form

     * handling action has been executed so as

     * to provide defaults for a form. The

     * form associated with the handler should be

     * shown to the end user.

     * <p/>

     * This result is also used if the given input

     * params are invalid, meaning the user

     * should try providing input again.

     */

    public static final String INPUT = "input";

 

    /**

     * The action could not execute, since the

     * user most was not logged in. The login view

     * should be shown.

     */

    public static final String LOGIN = "login";

2. type  (默认是dispatcher,类似于Servlet中的forward请求转发)

type值

使用说明

chain 

用来处理Action链,将一个action的执行与另外一个配置好的action串连起来。用第一个action的getter方法和第二个action的setter方法来完成action之间属性的复制。

dispatcher  

用来转向JSP页面,这是默认的结果类型,如果在action配置中没有配置其他的结果类型,它就会被使用

freemaker   

处理FreeMarker模板   

httpheader

控制特殊HTTP行为的结果类型

redirect  

   重定向到一个URL   

redirectAction 

  重定向到一个Action   

stream 

  向浏览器发送InputSream对象,通常用来处理文件下载,还可用于返回AJAX数据 

velocity

  处理Velocity模板 

xslt  

处理XML/XLST模板   

plainText   

   显示原始文件内容,例如文件源代码  

3.path    对应跟result对应的处理程序(一般为展示页面或者某个action)

path可以通过<result>的文本值直接指定!

全局result的配置实例

<package name="test" namespace="/ceshi" extends="struts-default">

     <global-results>

          <result name="success">/ok.jsp</result>

       <result name="ok" type="plainText">/aa.txt</result>

     </global-results>

        <action name="first" class="com.bjsxt.struts.test.FirstAction">

        </action>        

</package>

通配符配置Action

当配置文件中action较多时,并且配置都很类似。那么我们可以使用通配符来做合理的配置。在路径中使用多个*代替变化的部分,配置文件中使用{N}来访问url中*指定部分的值。

注:{N}, N为从1-9之间的值

将FirstAction对应的配置修改如下:

<action name="*_*" class="com.bjsxt.struts.test.{1}Action" method="{2}" >

          <result name="success">/{1}_{2}_ok.jsp</result>

</action>

将ok.jsp的名称改成:First_test2_ok.jsp。

地址栏输入:http://localhost/teststruts/ceshi/First_test2    (注意First中F是大写)

执行后,发现正常跳转到了success指定页面

猜你喜欢

转载自blog.csdn.net/weixin_38003467/article/details/83545587