Struts configures the wildcard * to match the method, implementing a dynamic call

Source: Internet
Author: User

01:web.xml in configuration, start struts2

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.4"xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "><!--01: Start Struts2 Frame--<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-pat Tern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</we Lcome-file> </welcome-file-list></web-app>

02: Writing The Action class

 Packagecom.self.action;/*** 02: Write the corresponding processing method*/ Public classhelloworldaction {PrivateString message;  PublicString helloworld_1 () { This. message= "Helloworld_1"; return"Helloworld_1"; }     PublicString helloworld_2 () { This. message= "Helloworld_2"; return"Helloworld_2"; }        //To inject a value into a property, you need to provide a set method     Public voidsetmessage (String message) { This. Message =message; }    //Show values on page, need get method     PublicString getMessage () {returnmessage; }}

03: Configure Struts.xml

<?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><!--01: Change. Action access to. Do and. Action--><constant Name= "Struts.action.extension" value= "do,action"/><!--02: Specify the default encoding, Equivalent to HttpServletRequest's Setcharacterencoding method, also used for freemarker, velocity output--><constant name= " Struts.i18n.encoding "value=" UTF-8 "/><include file=" Department.xml "/></struts>

  

04: Write the introduced department.xml

<?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=" dep "namespace="/department "extends=" Struts-default "><!--use wildcard * to refer to the method name, {1} represents the field represented by the first wildcard: this represents the method--><action name=" helloworld_* "class=" Com.self.action.HelloWorldAction "method=" {1} "><result name=" Helloworld_1 ">/helloworld_1.jsp</result ><result name= "Helloworld_2" >/helloworld_2.jsp</result></action></package></ Struts>

  

05: Writing Interface 1

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>

  

06: Writing Interface 2

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>

  

07: Access Path 1, 2

Http://localhost:8080/Struts2_01/department/helloworld_helloworld_1.action

Http://localhost:8080/Struts2_01/department/helloworld_helloworld_2.action

Struts configures the wildcard * to match the method, implementing a dynamic call

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.