Struts2 receives the parameters passed by the page

1. The first method is to obtain the parameters passed by the page in an attribute-driven manner. It should be noted here that the attribute name of the Action needs to be consistent with the parameter name of the page.
Action sample code:

public class LoginOneAction {
    
    
	
	private String userName;
	private String password;
	
	public String login(){
    
    
		System.out.println("userName="+userName);
		System.out.println("password="+password);
		if (userName.equals(password)) {
    
    
			return "success";
		}
		return "fail";
	}
	
	
	//=======setter AND getter方法==========
	public String getUserName() {
    
    
		return userName;
	}
	public void setUserName(String userName) {
    
    
		this.userName = userName;
	}
	public String getPassword() {
    
    
		return password;
	}
	public void setPassword(String password) {
    
    
		this.password = password;
	}
}

Configure LoginOneAction in struts.xml configuration, sample code:

<package name="user-login" namespace="/userLogin" extends="struts-default">
	<!-- action name请求的Action名称 class Action处理类对应具体路径 -->
	<!-- method 指定默认的方法名,如果不指定就是execute方法 -->
	<action name="loginOneAction" class="com.gx.web.LoginOneAction">
		<result name="success" type="dispatcher">/jsp/successOne.jsp</result>
		<result name="fail">/jsp/fail.jsp</result>
	</action>
</package>

Jsp page code (the attribute name of Action needs to be consistent with the parameter name of the page):

<body>
   <form action="${ctx}/userLogin/loginOneAction!login.action" method="get">
	  	userName:<input type="text" name="userName">
	  	<br>
	  	password:<input type="password" name="password">
	  	<br>
	  	<button type="submit">登录</button>    	
   </form>
 </body>

2. The second method, similar to the first method above, the attribute driver can also obtain the parameters passed by the page through JavaBean. First need to encapsulate the object, encapsulate the page parameters into a model object, sample code:

public class User implements Serializable{
    
    
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String userName;
	private String password;
	
	public String getUserName() {
    
    
		return userName;
	}
	public void setUserName(String userName) {
    
    
		this.userName = userName;
	}
	public String getPassword() {
    
    
		return password;
	}
	public void setPassword(String password) {
    
    
		this.password = password;
	}
}

In the properties of the first model object in the Action, and write a get and set method, sample code:

public class LoginTwoAction {
    
    
	private User user;
	
	//登录action方法
	public String login(){
    
    
		if (user.getUserName().equals(user.getPassword())) {
    
    
			return "success";
		}
		return "fail";
	}
	
	//======getter and  setter方法========
	public User getUser() {
    
    
		return user;
	}

	public void setUser(User user) {
    
    
		this.user = user;
	}
}

Configure LoginTwoAction in struts.xml configuration, sample code:

<package name="user-login" namespace="/userLogin" extends="struts-default">
	<action name="loginTwoAction" class="com.gx.web.LoginTwoAction">
		<result name="success">/jsp/successTwo.jsp</result>
		<result name="fail">/jsp/fail.jsp</result>
	</action>
</package>

The parameter name of the Jsp page is written differently from the first method. The ognl expression is used. Sample code:

<body>
   <form action="${ctx}/userLogin/loginTwoAction!login.action" method="get">
	   	userName:<input type="text" name="user.userName">
	   	<br>
	   	password:<input type="password" name="user.password">
	   	<br>
	   	<button type="submit">登录</button>
   </form>
 </body>

3. The third method is to accept the parameters passed by the jsp page in a model-driven manner. The implementation method: let Action implement a ModelDriven interface. This interface requires us to implement a getModel method, which returns the data model object used by the Action. The model object used here is the encapsulated object in the second method. Sample code:

public class LoginThreeAction implements ModelDriven<User> {
    
    
	private User user;

	@Override
	public User getModel() {
    
    
		if (user == null) {
    
    
			user = new User();
		}
		return user;
	}

	// 登录action方法
	public String login() {
    
    
		if (user.getUserName().equals(user.getPassword())) {
    
    
			return "success";
		}
		return "fail";
	}
	
	//========getter==========
	public User getUser() {
    
    
		return user;
	}
}

Configure LoginThreeAction in struts.xml configuration, sample code:

<package name="user-login" namespace="/userLogin" extends="struts-default">
	<action name="loginThreeAction" class="com.gx.web.LoginThreeAction">
		<result name="success">/jsp/successTwo.jsp</result>
		<result name="fail">/jsp/fail.jsp</result>
	</action>
</package>

The parameter name of the Jsp page needs to be consistent with the attribute name of the model object. Sample code:

<body>
   <form action="${ctx}/userLogin/loginThreeAction!login.action" method="get">
	   	userName:<input type="text" name="userName">
	   	<br>
	   	password:<input type="password" name="password">
	   	<br>
	   	<button type="submit">登录</button>
   </form>
 </body>

Guess you like

Origin blog.csdn.net/weixin_44547592/article/details/105773140