first, how to write an action in a struts2 frame
Three different ways:
1. Only a fixed method is required in the class:
publicexecute(){ return"..." }
不需要实现或者继承任何接口或者父类。 当客户端发出的请求和该action匹配到时就会自动执行 execute()方法。 注意: execute方法一定要返回String类型的对象,每一个字符串都可以对应一个跳转的页面.(字符串是可以自己随便定义的,字符串对应哪一个跳转的页面也是自己定义,在struts.xml文件中定义)
2. Implement a specified interface: com.opensymphony.xwork2.Action
There is only one abstract method in this interface public abstract String execute () throws Exception;
同时还有5个String类型的静态属性:ERROR SUCCESS INPUT NONE LOGIN
public static final String SUCCESS = " Success "; public static final String NONE = "NONE" ; public static final String error = "ERROR" ; public static final String input = "INPUT" ; public static final String login = "login" ;
3. Inherit from a specified parent class: Com.opensymphony.xwork2.ActionSupport
Note: This is the way we have been using it since.
Second, like before writing the Servlet class, after writing the action class needs to be configured in the Struts.xml file.
Configure the purpose and effect of the action in Struts.xml: Notifies the STRUTS2 framework that the class we wrote is an action, in the future the STRUTS2 framework will create objects for this class, invoke methods, and add more rich functionality to this action.
For example:
<packagename="Test"extends="Struts-default"Namespace="/test"> <actionname="MyTest"> <result>/index.jsp</result> </action> <actionname="Test1" class="Com.briup.web.action.ActionTest1"> <result name="Myfirstaction">/success.jsp</result> <result name="Myerror">/Error. jsp</result> </action> </package>
1)配置中<package>标签及其属性的介绍:
<package name="test"extends="struts-default" namespace="/test">
<package>: A struts.xml file can be configured with multiple <package> tags, a <package> tag can be configured with multiple <action> tags, a < The action> tag can be configured with multiple <result> name= "test" to give the current package a name of test function: 1. Uniquely identifies the current package. 2. It is convenient for the package and package to use this name to achieve the inheritance relationship. (like inheritance between classes and classes, to inherit from the name of the parent class) extends= "Struts-default" means that the current package inherits another package named Struts-default, This package is defined in the Struts-default.xml file in the core jar of STRUTS2, which defines a very default configuration. Note: All of the package that we define in Struts2 will inherit the Struts-default package directly or indirectly. (Like all classes in Java that inherit the object class directly or indirectly) namespace= "/test" means that the current package namespace is/test, and all the actions in the package will be accessed in the future. This namespace is added to the path. 2) configuration <action> tag properties and their sub-tags description: name= "Test1" indicates the current configuration the name of the action is test1, the name is random, Can be the same as or different from the name of the action class. In the future, the address bar in the browser will appear in the name to access the current action class class= "..." Indicates which Java class the currently configured action corresponds to, and we can configure an action, but this action does not correspond to any Java class. Just like the example of the mytest configured above, the .<result> tag indicates which jumps will be done after this action has been accessed. <result name= "Myerror" >/error.jsp</result> indicates the current action if the returned string is Myerror, then the page jumps to the/error.jsp note: each aAfter the ction is accessed, there is a return value for the string type, and the action will return the string by default, even if the action is accessed by any Java class: success (provided that the action class implements the action interface directly or indirectly) < Result name= "Success" >/index.jsp</result> equivalent to <result>/index.jsp</result> <result> The default value of the Name property in the label is success
Third, the action of the matching problem:
Generally there are two kinds: ① direct match ② dynamic call
The first type:
< package name="Schoolweibo" extends= "struts-default"> <action name="user_*" class="Indexaction " method ="{1}"> <result name="Success">/page/user_{1}.jsp</result> </Action></Package >
Where the "*" sign represents a placeholder, and "{1}" means the method in the action for which "*" is written, such as in a JSP page:
<a href="user_register.action">立即注册</a>
So for the Indexaction class is the Register method.
Let's look at a minimal configuration.
< package name="Schoolweibo" extends= "struts-default"> <action name="*_*" class="{1}action" method= ' {2} '> <result name="Success">/page/{1}_{2}.jsp</result> </Action></Package >
Here are two placeholders, the first representing the action class name, and the second representing the method name. If you have more placeholders, you can do the same.
The second type:
<package name = "Schoolweibo" extends =; <action name =< Span class= "Hljs-value" > "register" class = " Registeraction "; <result name =; /page/user_login.jsp</result ; </action , </package >
Attention:
If you use this dynamic call, you need to add
<constant name="struts.enable.DynamicMethodInvocation"value="true"/>
Typically a form is connected to the data that the action uses to process the form submission.
表单的 action 一般为: 在 struts.xml中配置的action中name的值!方法名.action<s:form action="register!register.action"> <s:textfield label="UserName" name="name"/> <s:submit/></s:form>
Then the Register method in the Registeraction class is called.
Special action configuration: Use wildcard characters to configure
Usage: Multiple action names or class names that would otherwise need to be shipped have a certain regularity.
例如:原来需要配置这样三个action:
<actionname="TESTCH1" class="COM.BRIUP.WEB.ACTION.ACTIONTESTCH1"> <result>/successch1.jsp</result> <result name="Error">/errorch1.jsp</result> </action> <actionname="TESTCH2" class="COM.BRIUP.WEB.ACTION.ACTIONTESTCH2"> <result>/successch2.jsp</result> <result name="Error">/errorch2.jsp</result> </action> <actionname="TestCh3" class="Com.briup.web.action.ActionTestCh3"> <result>/successch3.jsp</result> <result name="Error">/errorch3.jsp</result> </action>
这时候,我们就可以使用通配符配置一个action来代替这三个action:
name="test*"class="com.briup.web.action.ActionTest{1}"> <result>/success{1}.jsp</result> <resultname="error">/error{1}.jsp</result> </action>
{1} represents when the first * number that appears in the preceding Name property can be represented by {2} if it has a second * number.
Test* is the future in the browser to enter this action name, if the input test1, then this * number represents 1, if the input is Testlogin, then the * number is to represent login.
Note: If multiple actions that need to be configured do not have a very obvious rule, do not configure them this way.
Iv. characteristics of action and its access
Characteristics:
servlet是线程不安全的,因为servlet是单例。 struts2框架中的action是线程安全的,因为每次访问都会创建一个新的Action对象.所有在action里面可以随便的定义成员变量(只有成员变量才有线程安全的问题)
Access:
默认情况下,访问action的时候,使用namespace/actionName.action或者namespace/actionName就可以访问的到.
Attention:
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
例如:
<package name = "test" extends = "Struts-default" namespace =; < action name = "test1" Class = "Class A" ; < result ; /index.jsp</ result ; </action
>, </package ;
http://ip地址:端口号/项目名字/test/test1.action 或者 http://ip地址:端口号/项目名字/test/test1 访问成功页面就会跳转到index.jsp页面了但是由于不加任何后缀名也可以访问到这个action,这样容易和其他资源的访问产生冲突,比如servlet的访问(本来是访问项目的servlet的,但是Struts2框架却当成了这是访问action的请求来处理).所以我们需要配置一下,把action访问规定成,只能使用后缀名.action或者是.do 或者是其他形式来访问.//可以使用.action或者.do来访问<constant name="struts.action.extension" value="action,do"></constant>//只能使用.action<constant name="struts.action.extension" value="action"></constant>
V. When the action is accessed, the call to the method in action
1.默认情况下,访问action的时候会调用action中的execute方法,这个方法执行完返回一个字符串,然后根据字符串进行跳转.2.可以在<action>标签里面加上一个method属性,来指明将来访问这个action的时候会调用哪一个方法. 例如:<action name="test" class=".." method="login">注意:两个不同名字的action可以对应的是相同的一个java类. <action name="test1" class="类A" method="login"> <action name="test2" class="类A" method="register">这样使用test1和test2辆个不同名字的action就是访问到同一个类中的两个不同的方法:login和regist方法.但是login方法和register方法必须和execute方法类似的,都是返回String的值.3.地址栏中动态指定要调用的方法 例如:<package name="test" extends="struts-default" namespace="/test"> <action name="MyTest" class="类A"> <result>/index.jsp</result> </action> </package>类A里面有俩个方法:register login默认情况下地址栏中通过MyTest.action来访问这个action会访问到它的execute方法,同时我们还可以在地址栏中动态的指定要访问的方法:MyTest!login.action 这样就能访问到名字为MyTest的action对应类A中的login方法了.http://ip地址:端口号/项目名字/test/MyTest!login.action
Java EE Learning Note ssh-struts2 (2)