[Web Development Learning Notes] Structs2 Result learning notes (2) dynamic Result set
Result learning note (2)-do not forget to set the set get method for the saved value of the dynamic Result set.
Part 1: Code
// Frontend <% String context = request. getContextPath (); %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
//web.xml<struts> <constant name="struts.devMode" value="true" /> <package name="user" namespace="/user" extends="struts-default"> <action name="user" class="com.struts2.user.action.UserAction"> <result>${r}</result> </action> </package> </struts>
// Class package com. struts2.user. action; import com. opensymphony. xwork2.ActionSupport; public class UserAction extends ActionSupport {private int type; private String r; public String getR () {return r;} public void setR (String r) {this. r = r;} public int getType () {return type;} public void setType (int type) {this. type = type ;}@ Overridepublic String execute () throws Exception {if (type = 1) r = "/user_success.jsp"; else if (type = 2) r = "/user_error.jsp"; return "success ";}}
Part 2: Configuration Analysis web. xml:
Result:
<result>${r}</result>
Class package analysis:
private String r;public String execute() throws Exception {if(type == 1) r="/user_success.jsp";else if (type == 2) r="/user_error.jsp";return "success";}
The class package contains a member variable that dynamically determines the value of jsp based on other conditions. In the configuration file $ {r}, the value can be set in the value stack.
Conclusion: A property can be used to save a result;
The results can be determined dynamically;
In struct. xml, you can use <result >$ {r} </result> to enter the value;