再读Struts1.3.5 User Guide 4 Configuring Applications

第四部分 Configure Applications 

1. 本文主要讲述web.xml, struts-config.xml这些文件的配置。 

2. struts-config.xml中的<controller> element,这里面定义了ActionServlet的一些行为,有有关file upload的,http no-cache的等,具体可以看struts-config.dtd或参考本文中的列表。可以看到,上传文件默认的最大文件大小是250M,Struts默认会在用户的session中加入Locale信息,pagePattern, forwardPattern默认都是$M$P(module/path)等。 

3. message-resources element. 就是定义properties文件的basename。这没什么好说的了,值得一提的是其中有一个null的attribute,默认这一项是true,表示当一个key对应的value缺失的时候,返回null,这就会导致我们的页面出现异常页面,我们可以将这一项设置成false,这样,当一个key对应的value在properties文件中没有的时候,Struts会给出value是:???keyname???,这样就不会出异常界面,当我们调试程序的时候就可以一次性看到一共有多少个key丢失value,而不用一个一个异常页面的去看,然后修改了。 

<message-resources parameter="com.jointforce.resource.ApplicationResource" null="false" /> 

4. plugin configuration, 不说了。 

5. Configuring Your Application for Modules. 这个有点用。把一个应用配置成多个module,每个module就可以有自己的struts-xxx.xml的配置文件,而且对资源的引用都会套上module的一层路径,正好和我们的web应用的目录结构对应起来。 

要配置module很简单,首先,为每个module都准备一个xml文件(废话),然后,修改web.xml,这样: 

CODE: SELECT ALL
<init-param>
    <param-name>config</param-name>
    <param-value>/WEB-INF/struts-root.xml</param-value>
</init-param>
<init-param>
    <param-name>config/portal</param-name>
    <param-value>/WEB-INF/struts-portal.xml</param-value>
</init-param>
<init-param>
    <param-name>config/admin</param-name>
    <param-value>/WEB-INF/struts-admin.xml</param-value>
</init-param>


这就是EC2.0的配置了,分成普通用户的portal module和给管理员用的admin module,对于login,关于软件这些和普通用户,管理员没关系的action和页面,定义在struts-root.xml中就可以了,这也是struts的默认配置(config不带任何module)。

不过要千万注意的是,定义了module之后,我们的xml中的任何forward配置(global-forwards, action中的forward)中的path属性,前面都会套上module的prefix(要求这个path的值以slash /打头)。比如我们在portal module中有一个forward,他的path是/jsp/index.jsp,那么,到了URL中就变成了http://<domain name>/<our webapp context name>/portal/jsp/index.jsp了。其实这个行为就是在controller的配置中,forwardPattern的行为(forwardPattern的默认值是$M$P,除非我们手动修改这个配置,否则就是上面的那套规则)。

6. 既然有了Module,就存在module之间相互切换访问的问题了,比如现在我们的URL位置在portal module,想访问root module(默认module)中的index.jsp(返回登录页面),那怎么办呢?Struts介绍了三种做法,都非常简单可行:

(1) 使用Struts自带的extra中的SwitchAction,比如:
CODE: SELECT ALL
<action-mappings>
    <action path="/toModule"
    type="org.apache.struts.actions.SwitchAction"/>
    ...
</action-mappings>


这样,我们访问这个action,给出参数就OK了,比如这样访问:

http://localhost:8080/toModule.do?prefi ... =/index.do

这样,就访问到了moduleB的index.do了。

http://localhost:8080/toModule.do?prefi ... =/index.do

这样,就表示访问Struts的默认module(就是web.xml中光一个config的那个module)的index.do

(2) 在使用forward的时候配置contextRelative=true,比如:

CODE: SELECT ALL
<global-forwards>
    <forward name="toModuleB"
        contextRelative="true"
        path="/moduleB/index.do"
        redirect="true"/>
   ...
</global-forwards>


CODE: SELECT ALL
<action-mappings>
   <action ... >
       <forward       name="success"
           contextRelative="true"
           path="/moduleB/index.do"
           redirect="true"/>
   </action>
   ...
</action-mappings>


看到了么?也很简单,加了这个contextRelative之后,forward中path属性的值就会被理解成从当前的URI开始解析,如果当前的URI+path中定义的路径后,形成了另外一个module的URL,那么,Struts就会自动进入该module,读取该module的配置,使用该module的message resource。

(3) 在jsp文件中,如果是一个超链接,直接把module写上也可以:

<html:link module="/moduleB" path="/index.do"/>

表示访问moduleB的index.do

7. 要让Struts能工作,我们还需要配置web.xml:

CODE: SELECT ALL
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
        org.apache.struts.action.ActionServlet
    </servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>
         /WEB-INF/struts-config.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>


这表示在web应用启动的时候,装载Struts的ActionServlet类。在这里可以看到,可以给ActionServlet传递init参数,比如上述的指定Struts配置xml文件的参数。

还有一个参数是非常重要的,就是前面在“Building View Components”中提到的convertNull的parameter,如果我们要使用ValidateForm来动态生成带alidate功能的formbean(这样就不用我们自己写了)的话(这种formbean通过配置validate.xml来指定校验规则),要把这个convertNull设成true(默认值是false),将convertNull设成true之后,能解决这样的问题:

假设我们在HTML的form中有一项输入框,规定只能输入数字,不能输入字符串,那么,我们在formbean中肯定规定这项form-field是java.lang.Integer这样的类型,这种情况下,如果这个convertNull是false的话,那么,Struts就会给这个formbean的这个属性赋值为0,那么,如果用户这一项没有填,到了validate这一层的时候也不会出错(因为默认值是0,是个Integer)。所以,要把convertNull设成true,这样,Struts给这个formbean的这个属性赋值就是null,这样validate的时候就不会出现错误了。


CODE: SELECT ALL
<init-param>
    <param-name>convertNull</param-name>
    <param-value>
        true
    </param-value>
</init-param>


8. 还需要配置URL Mapping:

CODE: SELECT ALL
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>/do/*</url-pattern>
</servlet-mapping>


这表示访问http://www.mycompany.com/myapplication/do/logon的时候,就要调用/logon这个action,当然,我们见的更多的是这样的,也可以:

CODE: SELECT ALL
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>


一样的,访问http://www.mycompany.com/myapplication/logon.do就触发/logon这个action

9. 还需要在web.xml中配置taglib:

CODE: SELECT ALL
<taglib>
    <taglib-uri>/tags/app</taglib-uri>
    <taglib-location>/WEB-INF/app.tld</taglib-location>
</taglib>
  <!-- Struts Tag Library Descriptors -->
<taglib>
    <taglib-uri>/tags/struts-bean</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
    <taglib-uri>/tags/struts-html</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
......


10. 这样就基本OK了,xml文件的配置就是这些了,呵呵。当然了,我们还需要把struts中的所有jar文件都拷贝到WEB-INF/lib下(省的烦,不嫌烦可以根据需要拷贝)
原文地址:https://www.cnblogs.com/super119/p/1935000.html