Detailed explanation of the configuration file of Strust1

To use Strust1, you only need to configure two files: web.xml and struts-config.xml

web.xml

  • The web.xml file is mainly the configuration file for installing Strust1 .
  • In fact, the entry point for Struts is a servlet called ActionServlet. ActionServlet is the general controller, it is provided to us by struts, we don't need to write it ourselves, we just need to configure it. When accessing Struts for the first time, create an object instance of the ActionServlet class, and call the init method in the ActionServlet class to initialize (in fact, read the content in the struts-config.xml file, and according to the struts-config.xml in the content to initialize related resources). Therefore, it is necessary to install the ActionServlet in the web.xml file, and specify the location of struts-config.xml in the initialization parameter of ActionServlet.
<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/conf/struts-config.xml</param-value>  
    </init-param>  
    <load-on-startup>0</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

struts-config.xml

  • struts-config.xml is the core content of struts. As long as we want to use struts, we must configure the file struts-config.xml. All configuration parameters must be placed in struts-configthis root directory.
  • The most commonly used configurations are probably the following seven:

    • FormBean: <form-beans>Configured in the element.
    • Action mapping: <action-mappings>Configured in the element.
    • Global Forwards: <global-forwards>Configured in the element.
    • Global exception (Exception): <global-exceptions>Configuration in the element.
    • Controller (Controller): <controller>Configured in the element.
    • Information resource: <message-resources>Configuration in element.
    • Plugins: <plug-in>Configured in the element.
  • The most important things in daily development are FormBean and Action, which are the core of the entire Struts.

    <!-- 配置FormBean。 -->
<form-beans>
        <form-bean name="LoginActionForm"        type="com.webapp.form.LoginActionForm">
        </form-bean>
</form-beans>

The name here is the name of the Form, which is the only mark. This name will be used in the action. type is the path where the Form is located, which is composed of package name + class name.

<!-- 配置Action。 -->
<action-mappings>
        <action path="/Login" 
        name="LoginActionForm" 
        type="com.webapp.action.LoginAction"
        scope="request">
        <forward name="success" path="/LoginSuccess.jsp"></forward>
        <forward name="error" path="/LoginFailed.jsp"></forward>
        </action>
</action-mappings>

name is a unique identifier associated with a FormBean. path is the resource name to access the action in the future, generally http://localhost:8080/web/path. type is the path of Action, which consists of package name + class name. Scope is the scope of the specified FormBean, there are two kinds of session and request, the default is session. forward is a path jump.

  • Configure Global Forwards
    Global Forwards <global-forwards>is configured here.
<global-forwards>  
  <forward  name="forward1"  path="/a.do"/>  
  <forward  name="forward2"  path="/b.jsp"/>  
<global-forwards> 

name indicates the name of the forwarding or redirecting path. path represents the forwarding or redirecting path, which is generally a relative path.

  • Configure global exception (Exception)
    This element mainly configures exception handling, and its exception sub-element represents the global exception configuration. Struts operates exceptions by configuration, which is used to set the mapping between java exceptions and exception handling classes org.apache.struts.action.ExceptionHandler. It has seven properties as follows:
    1. className:指定和exception元素对应的配置类,默认为:org.apache.struts.config.ExceptionConfig。可有可无。
    2. Handler:指定异常得理类,默认为:org.apache.struts.action.ExceptionHandler。可有可无
    3. key:指定在Resource Bundle中描述该异常的消息key
    4. path:指定当异常发生时的转发路径。
    5. scope:指定ActionMessages实例的存放范围,可选值包括:request和session,默认为request。可有可无。
    6. type:指定所需处理异常类的名字,必须。
    7. bundle:指定Resource Bundle
<global-exceptions>  
  <exception   
      key="global.error.invalidlogin"  
      path="/error.jsp"  
      scope="request"  
      type="com.hn.tree"/>  
</global-exceptions> 
  • Configuration Controller (Controller)
    This element is used to configure the ActionServlet. It has the following properties.
      
   1. bufferSize:指定上载文件的输入缓冲大小,可选,默认为4096
  2. className:指定和controller元素对应的配置类,默认为org.apache.struts.config.ControllerConfig
  3. conentType:字符编码,如果在Action和JSP网页中设置了,则覆盖该设置。
  4. locale:指定是否把Locale对象保存到当前用户的session中,默认值为false.
  5. processorClass:指定负责请求的java类完整路径。
  6. tempDir:指定处理文件的临时工作目录,如果此项没有设置,将采用Servlet容器为web应用分配的临时工作目录。
  7. nochache:如果为true:在响应结果中将加入特定的头参数:Pragma,Cache-Control和Expise,防止页面被保存在客户端的浏览器中,默认为false
<controller  
  contentType="text/html;charset="UTF-8""  
  locale="true"  
  processorClass="con.ok"/> 
  • Configure information resources

Mainly configure the localized message text, which has the following properties.
  1. key: Specifies the attribute Key used in the ServletContext object stored in the Resource Bundle. The default is a string constant defined by Globals.MESSAGES_KEY. Only one Resource Bundle is allowed to use the default attribute Key.
  2. prameter: Specify the resource file name of MessageSources. If it is: abApplicationResources, the actual corresponding file path is: WEB-INF/classes/a/b/ApplicationResources.properties. 

<message-resources
            key="BRD"
            parameter="jp.co.dreamarts.insuite.resources.BRD_Resources" />
  • Configure the plugin
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
        <set-property property="definitions-config" value="/WEB-INF/tiles-def.xml" />
    </plug-in>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325476919&siteId=291194637