STRUTS2 Learning Notes (4)-use of wildcard characters _java

Source: Internet
Author: User

Wildcard is a very common configuration in the Struts2 configuration, in the process of development to follow the "agreement is better than the configuration" principle, in this principle, the agreed results, if more appropriate, will greatly reduce the number of configurations, making configuration becomes very simple and convenient.

Here is an example to illustrate:

1, by such a struts.xml configuration file:

Copy 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*" class= "com.bjsxt.struts2.action.StudentAction" method= "{1}" >
<result>/Student{1}_success.jsp</result>
</action>

<action name= "*_*" class= "Com.bjsxt.struts2.action". {1} Action ' method= ' {2} ' >
<result>/{1}_{2}_success.jsp</result>
<!--{0}_success.jsp-->
</action>
</package>
</struts>

The first action uses a single "*" Configuration, its method= "{1}", this {1} represents the first * in the previous Name property, and the second action,{2} represents the 2nd in the previous name attribute Similarly, the {1} in result in the first action is also the first * in the name attribute, as well as the second result.

2, if there is a index.jsp file

Copy Code code 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 ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 "/>
<title>insert title here</title>
<body>
Use wildcard characters to minimize the amount of configuration <br/>
<a href= "<%=context%>/actions/studentadd" > Add students </a>
<a href= "<%=context%>/actions/studentdelete" > Delete student </a>
<br/>
However, be sure to adhere to the "Agreement over configuration" principle
<br/>
<a href= "<%=context%>/actions/teacher_add" > Add teacher </a>
<a href= "<%=context%>/actions/teacher_delete" > Remove teacher </a>
<a href= "<%=context%>/actions/course_add" > Add Course </a>
<a href= "<%=context%>/actions/course_delete" > Remove course </a>

</body>

Then the studentadd_success.jsp file is invoked, based on the principle of the wildcard character, for the first <a></a>, which points to the Add method in the Studentaction class. Similarly for the third <a></a> it will point to the Teacheraction Add method and invoke the teacher_add_success.jsp file.

The use of wildcard characters makes the configuration of struts2 very simple, it also has its own principle that the accuracy of the match, the more accurate it is easier to match, for example, when the name of the two action can be matched to, it will automatically choose a more accurate match (at this time more accurate is not contain wildcard characters) , in the case of all contain wildcard characters, it seems that which one in front of the first match!

The above is Struts2 the use of the full content of the wildcard, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

Related Article

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.