标签:输出 cap import com 信息 程序 context row struts2
如,登陆表单login.jsp:
1 <form action="login" method="post"name="form1"> 2 3 4 用户名:<s:textfieldname="username"/><br/> 5 密 码:<s:passwordname="password"/><br/> 6 <s:submit value="提交"/> 7 </form>
1 private String username; 2 private String password; 3 4 public String getUsername() { 5 return username; 6 } 7 public void setUsername(String username) { 8 this.username = username; 9 } 10 public String getPassword() { 11 return password; 12 } 13 public void setPassword(String password) { 14 this.password = password; 15 }
1 public class loginAction extends ActionSupport{ 2 3 private Users users; 4 5 public Users getUsers(){ 6 7 return users; 8 9 } 10 11 public void setUsers(Users users){ 12 13 this.users=users; 14 15 } 16 17 }
<form action="login" method="post"name="form1"> 用户名:<s:textfieldname="users.username"/><br/> 密 码:<s:passwordname="users.password"/><br/> <s:submit value="提交"/> </form>
public class loginAction extends ActionSupport implementsModelDriven<Users>{ private Users users =new Users(); public Users getModel(){ return users; } }
1 <table border="1" width="360"> 2 <caption> 3 作者李刚的图书 4 </caption> 5 <!-- 迭代输出ValueStack中的books对象,其中status是迭代的序号 --> 6 <s:iterator value="books" status="index"> 7 <s:if test="#index.odd == true"> 8 <tr style=" 9 </s:if> 10 <s:else> 11 <tr> 12 </s:else> 13 <td> 14 书名: 15 </td> 16 <td> 17 <s:property /> 18 </td> 19 </tr> 20 </s:iterator> 21 </table>
Struts2将所有属性值封装在struts.valueStack请求属性里,可以通过request.getAttribute("struts.valueStack")获取。Action所有的属性都被封装到了ValueStack对象中,它类似于map,Action中的属性名可以理解为ValueStack中value的名字。可以通过valueStack.findValue("name")来取值。
BookService.Java
public class BookService { // 模拟数据库 private String[] books = new String[] { "疯狂Java讲义" , "轻量级Java EE企业应用实战", "疯狂Ajax讲义", "疯狂XML讲义", "Struts 2权威指南" }; public String[] getLeeBooks() { return books; } }
GetBooksAction.java
1 import com.opensymphony.xwork2.Action; 2 import com.opensymphony.xwork2.ActionContext; 3 4 public class GetBooksAction implements Action 5 { 6 private String[] books; 7 8 public void setBooks(String[] books) 9 { 10 this.books = books; 11 } 12 13 public String[] getBooks() 14 { 15 return books; 16 } 17 18 public String execute() throws Exception 19 { 20 String user = (String)ActionContext.getContext().getSession().get("user"); 21 if (user != null && user.equals("crazyit")) 22 { 23 BookService bs = new BookService(); 24 setBooks(bs.getLeeBooks()); 25 return SUCCESS; 26 } 27 else 28 { 29 return LOGIN; 30 } 31 } 32 } 33 34 35 36 37 showBooks.jsp 38 39 <table border="1" width="360"> 40 41 <% 42 43 // 获取封装输出信息的ValueStack对象 44 ValueStack vs = (ValueStack)request.getAttribute("struts.valueStack"); 45 46 // 调用ValueStack的fineValue方法获取Action中的books属性值 47 String[] books = (String[ ])vs.findValue("books"); 48 49 //迭代输出全部图书信息 50 for (String book : books){ 51 %> 52 53 54 <tr> 55 <td>书名:</td> 56 <td><%=book%></td> 57 </tr> 58 <%}%> 59 60 61 </table>
Struts2中jsp前台传值到action后台的三种方式以及valueStack的使用
标签:输出 cap import com 信息 程序 context row struts2
原文地址:http://www.cnblogs.com/printN/p/6502038.html