Many core functions of struts2 are implemented by the interceptor. The interceptor and result types are defined in the struts-default.xml. Therefore, many core functions provided by struts2 cannot be used without inheriting the Struts-default package. 1. the name attribute must be specified when the package tag is used to configure the package. The attribute value can be named randomly but must be unique. It has no correspondence with Java class packages. To inherit the package from other packages, you must use this attribute to reference the package. The namespace attribute of the package is used to define the namespace of the package. The namespace is part of the path to access the action under the package. This attribute can be left empty. If not configured, the default namespace is "" (Null String ). 2. In struts2, the URL path for accessing the action in struts2 is composed of two parts: Package namespace + action name, for example, the URL of the action named helloworld in the previous article is/test/Bbbb.
Note:If no class is specified for action, actionsupport is used by default. The default processing of the execute () method of actionsupport is to return a "success" string. The method attribute is used to specify the method in the execution action. If it is not specified, the default value is "execute", that is, the execute method of the specified action is executed.
3. Result tagThe result tag is used to specify a view. The name attribute specifies the view name. If the name attribute is not specified, the default value is success (the value of the constant success is the string constant "success "). Struts2 provides multiple view direction types, which are specified by the Type attribute. For example, dispatcher: Request Forwarding (default) Redirect: redirect to page redirectaction: redirect to action plaintext: the source code to be switched to the resource is displayed as is, rather than the execution result of the Code (the results can be tested in Firefox and IE8, but not in IE6 ). Not commonly used.
Note,For pages that request forwarding, it can be pages in the WEB-INF; the redirected page cannot be pages in the WEB-INF. Because redirection is equivalent to sending a request again, the user cannot directly access resources in the WEB-INF.
In the previous article 2. In the first struts2 Program-helloworld program,
The Struts. xml configuration is as follows:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="aaaa" namespace="/test" extends="struts-default"> <action name="bbbb" class="actions.HelloWordAction"> <result>/welcome.jsp</result> </action> </package></struts>
The hellowordaction. Java code is as follows:
package actions;public class HelloWordAction { public String execute(){ System.out.println("I am HelloWordAction.java"); return "success"; }}
If you want to modify hellowordaction. Java:
package actions;public class HelloWordAction { public String some(){ System.out.println("I am HelloWordAction.java"); return "other"; }}
You must modify the Struts. xml file configuration as follows:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="aaaa" namespace="/test" extends="struts-default"> <action name="bbbb" class="actions.HelloWordAction" method="some"> <result name="other">/welcome.jsp</result> </action> </package></struts>
3. struts2 configuration file label Introduction