For example, there are multiple requests in index.jsp
<a href="<%=path %>/usersAction!add.action">add</a><br>
<a href="<%=path %>/usersAction!show.action">show</a><br>
<a href="<%=path %>/usersAction!update.action">update</a><br>
<a href="<%=path %>/usersAction!delete.action">delete</a><br>
action inside
package com.cj.action;
import com.opensymphony.xwork2.ActionSupport;
public class UsersAction extends ActionSupport{
public String add() throws Exception {
System.out.println("Get into add");
return SUCCESS;
}
public String show() throws Exception {
System.out.println("Get into show");
return SUCCESS;
}
public String update() throws Exception {
System.out.println("Get into update");
return SUCCESS;
}
public String delete() throws Exception {
System.out.println("Get into delete");
return SUCCESS;
}
}
struts.xml configuration
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <! - Configuration and labeling - > <struts> <! - exclamation mark mode (need to be opened), the official website does not recommend the use of this mode, we recommend that you do not use. In this way, a switch needs to be turned on first - >. <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <! - Used in development mode, so that more detailed error messages can be printed out - >. <constant name="struts.devMode" value="true"></constant> <! - Defau lt View Theme --> ___________ <constant name="struts.ui.theme" value="simple"></constant> <! -- name attribute: package name, used for invocation or inheritance by other packages extends: Which package to inherit, the configuration information and interceptors under the package, and so on namespace: optional, url connection must be added / new/action.xxx --> <package name="test" extends="struts-default"> <! -- Action corresponds to the previous concept of servlet and corresponds to the url address of a request name. localhost:8080 / project name / new/login.do --> <action name="usersAction" class="com.cj.action.UsersAction" method="add"> <result name="success">/index.jsp</result> </action> <action name="usersAction" class="com.cj.action.UsersAction" method="show"> <result name="success">/index.jsp</result> </action> <action name="usersAction" class="com.cj.action.UsersAction" method="update"> <result name="success">/index.jsp</result> </action> <action name="usersAction" class="com.cj.action.UsersAction" method="delete"> <result name="success">/index.jsp</result> </action> </package> </struts>
Like xml, where there are multiple duplicate files, wildcards are needed
<action name="usersAction" class="com.cj.action.UsersAction" method="{*}">
<result name="success">/index.jsp</result>
</action>
You can complete the above four requests
But if there are more than one action, there is only one user on it, and if there is another news, there will be one more.
<action name="usersAction" class="com.cj.action.UsersAction" method="{*}">
<result name="success">/index.jsp</result>
</action>
<action name="newsAction" class="com.cj.action.NewsAction" method="{*}">
<result name="success">/index.jsp</result>
</action>
There will be two more similarities, how to merge the two similarities?
Add news additions and deletions to index.jsp
<a href="<%=path%>/NewsAction!add.action">add</a>
<br>
<a href="<%=path%>/NewsAction!show.action">show</a>
<br>
<a href="<%=path%>/NewsAction!update.action">update</a>
<br>
<a href="<%=path%>/NewsAction!delete.action">delete</a>
<br>
struts.xml
Represent all {1} for the first * number by substitution numbers for news and users
<action name="*Action" class="com.cj.action.{1}Action" method="{*}">
<result name="success">/index.jsp</result>
</action>
The request address in index.jsp should also be changed accordingly
No wildcards*
<a href="<%=path%>/usersAction!add.action">add</a>
Use wildcards*
You have to replace it with a class name.
<a href="<%=path%>/UsersAction!add.action">add</a>
Further optimization
_ Which class and which method
<action name="*_*" class="com.cj.action.{1}Action" method="{2}">
<result name="{1}success">/{1}index.jsp</result>
</action>
jsp inside
<a href="<%=path%>/News_add!add.action">add</a>