1. One of the ways to change the action attributes in the form form in the DMI dynamic method call has been explained. There are two other types. One is to change the method attribute in the action tag in the struts.xml configuration file to specify the execution of different methods to handle different business logic; the other is to use wildcards. To change the method attribute, multiple actions need to be configured, and most of these actions are defined the same, so this definition is quite redundant. Therefore, using wildcards can replace multiple logically processed Actions in one action tag.
2. Demonstration: ( It is similar to the previous dynamic method call to change the form form action attribute, and a small modification was made to the struts.xml configuration file.)
The requirements still have not changed. Click different submit buttons to submit the same form and hand over the different services to the same Action processing class for processing.
⒈First display a form, there are two submit buttons in the form, but they represent different businesses. The user logs in when clicking to log in; the user logs in when clicking to register.
⒉User login:
⒊User registration:
The specific code is as follows:
⑴. Login to the registration page (index.jsp):
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script src="js/jquery-1.7.2.js"></script><title>index</title><script type="text/javascript">$(function(){$("input:eq(3)").click(function(){/*Dynamicly modify the value of the action attribute in the form to submit the registered request to the Action class*/ $("#form").attr("action","Create");});});</script></head><body><form action="Login" method="post" id="form">Name: <input type="text" name="name" /><br><br>Password: <input type="password" name="password" /><br><br><input type="submit" value="Login"><input type="submit" value="Register"></form></body></html> ⑵. Code of struts.xml configuration file
<?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"><struts><package name="myP" extends="struts-default"><action name="*" method="{1}"><result name="userLogin">WEB-INF/jsp/userLogin.jsp</result><result name="userCreate">WEB-INF/jsp/userCreate.jsp</result></action></package></struts> Analysis:
1. In this configuration file, a wildcard character is configured for the name attribute in the action tag: "*", and the value of the method attribute afterward is: {1}.
2. It means that when the user clicks the login button on the index.jsp page, the action="Login" request in form: will be passed to struts. Because wildcard configuration is performed in struts.xml, "*" is regarded as "Login", that is, name="Login". The subsequent method value is: {1} represents the first "*", which is method="Login". So struts will find the Login method in the action.Action class and call it. If the user clicks the registration button, then the process is the same as clicking the login button. You can write a small example to experience it.
⑶. Code of Action class:
package action;import com.opensymphony.xwork2.ActionSupport;public class Action extends ActionSupport {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public String Login(){System.out.println("UserLogin");return "userLogin";}public String Create(){System.out.println("User Registration");return "userCreate";}} Of course, the use of wildcard characters is not just that simple, but may also include: "*-*", "Book_*", etc. These wildcard characters can be used in the properties of the action tag in the struts.xml configuration file, such as mthod and class attributes, or in the result tag, as follows:
<!--Define a general action tag--><action name="*"><!--Define Result tag using expressions--><result>/WEB-INF/jsp/{1}.jsp</result></action> In the above action definition, the name of the action is a *, so it can match any action. All requests are handled through this action. Because this action does not have a class attribute, the ActionSupport class is used to handle it. Because there is no method attribute, the default is the execute method and returns the success string. Moreover, the name attribute in the result tag is success by default, so the Action always directly returns the jsp resource specified in the result. Therefore, the meaning of the above action definition is: if the user requests a.action, then jump to a.jsp; if b.action is requested, then jump to b.jsp.
The above is the dynamic method call in struts introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!