Wildcards are a very commonly used configuration method in struts2 configuration. In the process of program development, the principle of "convention is better than configuration" must be followed. Under this principle, if the result of the agreement is more appropriate, the number of configurations will be greatly reduced, making the configuration very simple and convenient.
Here is an example to illustrate:
1. By such a struts.xml configuration file:
Copy the code code 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>
<constant name="struts.devMode" value="true" />
<package name="actions" extends="struts-default" namespace="/actions">
<action name="Student*" method="{1}">
<result>/Student{1}_success.jsp</result>
</action>
<action name="*_*" method="{2}">
<result>/{1}_{2}_success.jsp</result>
<!-- {0}_success.jsp -->
</action>
</package>
</struts>
The first action uses a single "*" configuration method, its method="{1}", this {1} represents the first * in the previous name attribute. Compared with the second action, {2} represents the second * in the previous name attribute; similarly, the result in the first action also represents the first * in the name attribute, which is also similar to the second result.
2. If there is an index.jsp file
The code copy is as follows:<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<% String context = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
Use wildcards to minimize configuration amount<br />
<a href="<%=context %>/actions/Studentadd">Add students</a>
<a href="<%=context %>/actions/Studentdelete">Delete students</a>
<br />
However, we must abide by the principle of "convention is better than configuration"
<br />
<a href="<%=context %>/actions/Teacher_add">Add teacher</a>
<a href="<%=context %>/actions/Teacher_delete">Delete the teacher</a>
<a href="<%=context %>/actions/Course_add">Add courses</a>
<a href="<%=context %>/actions/Course_delete">Delete courses</a>
</body>
</html>
Then according to the principle of wildcards, for the first <a></a>, its pointer is the add method in the StudentAction class, the Studentadd_success.jsp file will be called. Similarly, for the third <a></a>, it will point to the add method of TeacherAction and call the Teacher_add_success.jsp file.
The use of wildcards makes the configuration of struts2 very simple. It also has its own principle, namely: accuracy matching, the more accurate it is, the easier it is to match. For example, when both actions can be matched, it will automatically select a more accurate match (the more accurate value at this time is that there is no wildcard). When both wildcards are included, it seems that which one matches first!
The above is the full content of the use of the wildcard characters of Struts2. I hope it can give you a reference and I hope you can support Wulin.com more.