Configuration of the Struts1

Source: Internet
Author: User

The Web. xml file is required for configuring any Java Web application.
(1) Configuring the Actionservlet of Struts
<servlet> elements to declare actionservlet
<servlet-name> elements: Used to define the name of the servlet
<servlet-class> elements: Used to specify the full class name of the servlet

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
</servlet>

Also configure the <servlet-mapping> element, which is used to specify which URLs Actionservlet can handle
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

Note: (1) <servlet-name>?</servlet-name> in <servlet-mapping> and <servlet> to fill in the same, it is a line, the < Servlet-mapping> and <servlet> are joined together.
(2) There can be only one servlet in the Struts framework, because the servlet supports multithreading. and <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> The Actionservlet in the Struts.jar package is imported when the struts package is imported.
(3) All URLs submitted with *.do as extensions in the display layer are referred to this servlet for processing. *.do can be written in any form you want, such as/do/*, at which point the servlet processes all URLs prefixed with "/do", such as Http://localhost:8080/helloapp/do/helloWord,
Extensions (1) We can inherit Org.apache.struts.action.ActionServlet to get the subclasses we can extend, overriding a method init () in the subclass. At this point in <servlet-class>?</servlet-class>? is the path to our new class, and only one exists.

(2) Declaring the initialization parameters of Actionservlet
Initialization parameters are used to initially configure the runtime environment of the servlet. <servlet> 's <init-param> child elements are used to configure servlet initialization parameters.
Config: Indicates the configuration file location of the struts application as a relative path, if not set, the default value is/web-inf/struts-config.xml
Debug: Sets the debug level of the servlet to control the verbosity of the logging. The default is 0, which records relatively minimal log information.
Detail: Set the debug level of the Digester, Digester is a framework used by the struts framework to parse the XML configuration file, which allows you to view the parsing logs for different levels of detail. The default is 0, which records relatively minimal log information.
<load-on-startup>? </load-on-startup> in? The value of the Actionservlet is the order in which this is loaded when the server is opened, and the lower the value, the more loaded.

<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>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>

Extension: (1) When the server is started, load Actionservlet, and Actionservlet will call the relevant method to go, according to the parameters below it's initial value, the parameters in this actionservlet the line initialization
(2) When multiple people collaborate on a project, the STRUTST configuration file can be expanded appropriately, but it must start with CONFIG. Such as
<init-param>
<param-name>config/XXXXXXXXX</param-name>
<param-value>/WEB-INF/XXXXX.xml</param-value>
</init-param>

(3) Configuration Welcome file list
When a customer accesses a web app, if only the root URL of the web app is given, no specific file name is specified. The Web container automatically calls the Web app's welcome file. <welcome-file-list> is used to set this item.
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
Description: You can have multiple <welcom-file> under <welcome-file-list>. The Web container will look for the Welcome screen in turn until it is found. However, if it does not exist, an "HTTP 404 Not Found" error message is returned to the client.

Because servlet mappings cannot be configured in the <welcome-file-list> element, the struts action cannot be directly used as a welcome file. However, it can be configured through global forwarding items in struts.
A:welcome.jsp page (can change file name)
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-logic" prefix= "logic"%>
<body>
<logic:forward name= "Welcome"/>
</body>
B:web.xml
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
C:struts-config.xml
<global-forwards>
<forward name= "Welcome" path= "Hello.do" >
</global-forwards>
I understand, for reference only
Operating mechanism: When the server starts, it loads the welcome.jsp in B <welcome-file>welcome.jsp</welcome-file>, then reads the < in (A) welcome.jsp Logic:forward name= "Welcome"/&GT, and next goes to the <forward name= "Welcome" path= "Helloworld.do" in C;
This is because it ends in. Do (hello.do), so it will find the <action> match in the C file, and finally go to this <action> JSP page.

Configuration of the Struts1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.