剛開始學習的人非常有用:struts2中將jsp數據傳到action的幾種方式
阿新 • • 發佈:2017-06-29
登錄頁面 sign config print log println amp war htm
先給上struts.xml代碼:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="login" namespace="/user" extends="struts-default"> <action name="*_*" class="com.wepull.struts2.action.{1}Action" method="{2}"> <result name="success">{1}_{2}.jsp</result> <result name="input">User_toLogin.jsp</result> </action> </package> </struts>
import com.opensymphony.xwork2.ActionSupport; /** * 方式一:直接在action中寫屬性。然後寫set、get方法 * 1.在Action裏加入屬性,屬性名要相應jsp上的屬性名 * 2.加入相應的set方法 * */ public class UserAction extends ActionSupport { private String name; private String pass; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public String doLogin(){ String forward="input"; //System.out.println(name+" "+pass); if("wepull".equalsIgnoreCase(name)&&"123".equalsIgnoreCase(pass)){ return SUCCESS; //登錄成功,返回成功頁面 }else{ return forward; //登錄失敗,返回又一次登錄 } } public String toLogin(){ return SUCCESS; //進入登錄頁面 } }
<form action="user/User_doLogin"> <table> <tr> <td>username</td> <td><input type="text" name="name"></td> <td> </td> </tr> <tr> <td>密 碼</td> <td><input type="text" name="pass"></td> <td> </td> </tr> <tr> <td><input type="submit" value="sure" ></td> <td><input type="reset" value="reset" ></td> <td> </td> </tr> </table> </form>
import com.opensymphony.xwork2.ActionSupport; /** * 方式二:把屬性封裝到dto中,傳值時以dto名字.當中屬性 * 1.在Action裏加入屬性,屬性名要相應jsp上的屬性名 * 2.加入相應的set方法 * */ public class UserAction extends ActionSupport { private UserDTO user; public UserDTO getUser() { return user; } public void setUser(UserDTO user) { this.user = user; } public String doLogin(){ String forward="input"; //System.out.println(user.getName()+" "+user.getPass()); if("wepull".equalsIgnoreCase(user.getName())&&"123".equalsIgnoreCase(user.getPass())){ return SUCCESS; }else{ return forward; } } public String toLogin(){ return SUCCESS; } } ------------------------------------------------------------------------------------------------------------------------------ UserDTO.java代碼 public class UserDTO { private String id; private String name; private String pass; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } }
<form action="user/User_doLogin"> <table> <tr> <td>username</td> <td><input type="text" name="user.name"></td> <td> </td> </tr> <tr> <td>密 碼</td> <td><input type="text" name="user.pass"></td> <td> </td> </tr> <tr> <td><input type="submit" value="sure" ></td> <td><input type="reset" value="reset" ></td> <td> </td> </tr> </table> </form>
import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; /** * 方式三:實現ModelDriven * 實現ModelDriven接口 * */ public class UserAction extends ActionSupport implements ModelDriven<UserDTO>{ private UserDTO user; public UserDTO getUser() { return user; } public void setUser(UserDTO user) { this.user = user; } public String doLogin(){ String forward="input"; //System.out.println(user.getName()+" "+user.getPass()); if("wepull".equalsIgnoreCase(user.getName())&&"123".equalsIgnoreCase(user.getPass())){ return SUCCESS; }else{ return forward; } } public String toLogin(){ return SUCCESS; } public UserDTO getModel() { if(user==null){ user=new UserDTO(); } return user; } }
<form action="user/User_doLogin"> <table> <tr> <td>username</td> <td><input type="text" name="name"></td> <td> </td> </tr> <tr> <td>密 碼</td> <td><input type="text" name="pass"></td> <td> </td> </tr> <tr> <td><input type="submit" value="sure" ></td> <td><input type="reset" value="reset" ></td> <td> </td> </tr> </table> </form>
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; /** * 獲取request參數的方式 *HttpServletRequest request 傳參方式 * */ public class UserAction extends ActionSupport{ public String doLogin(){ //獲取request對象 HttpServletRequest request=ServletActionContext.getRequest(); //獲取session對象 //HttpSession session=request.getSession(); String name=request.getParameter("name"); String pass=request.getParameter("pass"); String forward="input"; if("wepull".equalsIgnoreCase(name)&&"123".equalsIgnoreCase(pass)){ return SUCCESS; }else{ return forward; } } public String toLogin(){ return SUCCESS; } }
剛開始學習的人非常有用:struts2中將jsp數據傳到action的幾種方式