在jsp頁面提交到action中的表單元素在action中是以對象的形式存在的, action中的對象name必須與jsp頁面中的表單元素name相同,struts框架自動為action的對象賦值. 一.普通驅動 註意:action中的對象name必須與jsp頁面中的表單元素name相同,strut ...
在jsp頁面提交到action中的表單元素在action中是以對象的形式存在的,
action中的對象name必須與jsp頁面中的表單元素name相同,struts框架自動為action的對象賦值.
一.普通驅動
註意:action中的對象name必須與jsp頁面中的表單元素name相同,struts框架自動為action的對象賦值.
案例:
struts.xml文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 5 "http://struts.apache.org/dtds/struts-2.3.dtd"> 6 7 <struts> 8 <package name="Hello" extends="default-struts" namespace=""> 9 <action name="Hello" class="com.ahd.action.HelloAction"> 10 <result>/success.jsp</result> 11 </action> 12 </package> 13 </struts>
jsp頁面:
1 <body> 2 <form action="hello.action"> 3 用戶名:<input type="text" name="username"/><br/> 4 密 碼:<input type="password" name="password"/><br/> 5 <input type="submit" value="submit"/> 6 <input type="reset" name="reset"/><br/> 7 </form> 8 9 </body>
action頁面:
1 package com.ahd.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class HelloAction extends ActionSupport{ 6 private String username; 7 private String password; 8 9 @Override 10 public String execute() throws Exception { 11 // TODO Auto-generated method stub 12 return super.execute(); 13 } 14 15 public String getUsername() { 16 return username; 17 } 18 19 public void setUsername(String username) { 20 this.username = username; 21 } 22 23 public String getPassword() { 24 return password; 25 } 26 27 public void setPassword(String password) { 28 this.password = password; 29 } 30 31 }
二.屬性驅動
普通驅動如果是大項目表單提供過多元素action會比較亂,屬性驅動就是將表單提供的元素封裝到一個對象中,這個對象作為action中的屬性,
在前端頁面表單中需要提交的元素name統一寫成 對象.name
案例:
實體類User
1 package com.ahd.entity; 2 3 public class User { 4 private String username; 5 private String password; 6 public String getUsername() { 7 return username; 8 } 9 public void setUsername(String username) { 10 this.username = username; 11 } 12 public String getPassword() { 13 return password; 14 } 15 public void setPassword(String password) { 16 this.password = password; 17 } 18 }
HelloAction:
1 package com.ahd.action; 2 3 import com.ahd.entity.User; 4 import com.opensymphony.xwork2.ActionSupport; 5 6 public class HelloAction extends ActionSupport{ 7 private User user; 8 9 @Override 10 public String execute() throws Exception { 11 // TODO Auto-generated method stub 12 System.out.println(user.getUsername()+user.getPassword()); 13 return super.execute(); 14 } 15 16 public User getUser() { 17 return user; 18 } 19 20 public void setUser(User user) { 21 this.user = user; 22 } 23 24 }
index.jsp:
1 <body> 2 <form action="hello.action"> 3 用戶名:<input type="text" name="user.username"/><br/> 4 密 碼:<input type="password" name="user.password"/><br/> 5 <input type="submit" value="submit"/> 6 <input type="reset" name="reset"/><br/> 7 </form> 8 9 </body>
strust.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 5 "http://struts.apache.org/dtds/struts-2.3.dtd"> 6 7 <struts> 8 <package name="Hello" extends="default-struts" namespace=""> 9 <action name="Hello" class="com.ahd.action.HelloAction"> 10 <result>/success.jsp</result> 11 </action> 12 </package> 13 </struts>