SSH框架Action向页面传值差异
SSH框架Action向页面传值差异
SSH框架Action向页面传值方式(本次主要介绍传值方式与jsp取值写法的差异):
方式 一:在Action中声明一个List变量,生成get/set方法,通过自定义的action中的方法往List集合变量中赋值,再在jsp页面通过<s:iterator value="" var="">取值;
方式 二:在Action方法内声明一个List变量,然后通过数据库查询将结果用List集合变量存储,之后通过ServletActionContext.getRequest().setAttribute("list", list);将集合通过request传值到jsp页面,再在jsp页面通过<s:iterator value="" var="">取值;
ServletActionContext.getRequest().setAttribute("list", list)
等同于:HttpServletRequest=ServletActionContext.getRequest();获取request请求
request.setAttribute("list", list);将list集合放入request请求,"list"表示页面取值的name或Id;list表示当前Action的查询结果集;
方式 一Action中get|set传值:
action:
1 package top.malloc.ssh.action; 2 3 import java.util.List; 4 5 import javax.servlet.http.HttpServletRequest; 6 7 import org.apache.struts2.ServletActionContext; 8 9 import top.malloc.ssh.bean.TbBook; 10 import top.malloc.ssh.service.ITbBookService; 11 12 public class BookAction { 13 14 private ITbBookService bookService; 15 16 private TbBook tbbook; 17 //申明List 18 private List< TbBook> list; 19 //产生get|set 20 public List<TbBook> getList() { 21 return list; 22 } 23 24 public void setList(List<TbBook> list) { 25 this.list = list; 26 } 27 28 public ITbBookService getBookService() { 29 return bookService; 30 } 31 32 public void setBookService(ITbBookService bookService) { 33 this.bookService = bookService; 34 } 35 36 public TbBook getTbbook() { 37 return tbbook; 38 } 39 40 public void setTbbook(TbBook tbbook) { 41 this.tbbook = tbbook; 42 } 43 44 public String addBook(){ 45 bookService.save(tbbook); 46 System.out.println("success!"); 47 return "success"; 48 } 49 50 public String selectById(){ 51 bookService.findById(tbbook.getBookid()); 52 return "success"; 53 } 54 //action自定义方法,查询赋值 55 public String selectAll(){ 56 list=bookService.findAll(); 57 return "list"; 58 } 59 60 public String delBook(){ 61 bookService.delete(tbbook); 62 return "success"; 63 } 64 65 public String updBook(){ 66 67 return "success"; 68 } 69 70 71 }
private List< TbBook> list;
public List<TbBook> getList() {
return list;
}
public void setList(List<TbBook> list) {
this.list = list;
}
public String selectAll(){
//数据库查询赋值
list=bookService.findAll();
return "list";
}
jsp取值:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags"%> 3 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 <title>list.jsp</title> 8 </head> 9 10 <body> 11 <s:form action="selectAll_book.action" method="post"> 12 <h4 align="center">书籍查询</h4> 13 <table border="3" align="center"> 14 <tr> 15 <td>书名</td> 16 <td>作者</td> 17 <td>ISBN</td> 18 <td>出版社</td> 19 </tr> 20 <s:iterator value="list" var="book"> 21 <tr> 22 <td><s:property value="#book.bookname" /></td> 23 <td><s:property value="#book.author" /></td> 24 <td><s:property value="#book.isbn" /></td> 25 <td><s:property value="#book.publish" /></td> 26 </tr> 27 <br /> 28 </s:iterator> 29 </table> 30 </s:form> 31 </body> 32 </html>
get|set取值写法:<s:iterator value="list" var="book">
方式 二request传值:
1 package top.malloc.ssh.action; 2 3 import java.util.List; 4 import org.apache.struts2.ServletActionContext; 5 import top.malloc.ssh.bean.TbBook; 6 import top.malloc.ssh.service.ITbBookService; 7 8 public class BookAction { 9 10 private ITbBookService bookService; 11 12 private TbBook tbbook; 13 14 public ITbBookService getBookService() { 15 return bookService; 16 } 17 18 public void setBookService(ITbBookService bookService) { 19 this.bookService = bookService; 20 } 21 22 public TbBook getTbbook() { 23 return tbbook; 24 } 25 26 public void setTbbook(TbBook tbbook) { 27 this.tbbook = tbbook; 28 } 29 30 public String addBook(){ 31 bookService.save(tbbook); 32 System.out.println("success!"); 33 return "success"; 34 } 35 36 public String selectById(){ 37 bookService.findById(tbbook.getBookid()); 38 return "success"; 39 } 40 41 public String selectAll(){ 42 //申明list集合变量 43 List list=bookService.findAll(); 44 //将集合存入request请求 45 ServletActionContext.getRequest().setAttribute("list", list); 46 return "list"; 47 } 48 49 public String delBook(){ 50 bookService.delete(tbbook); 51 return "success"; 52 } 53 54 public String updBook(){ 55 56 return "success"; 57 } 58 59 60 }
public String selectAll(){
//申明list集合变量
List list=bookService.findAll();
//将集合存入request请求
ServletActionContext.getRequest().setAttribute("list", list);
return "list";
}
jsp取值:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags"%> 3 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 <title>list.jsp</title> 8 </head> 9 10 <body> 11 <s:form action="selectAll_book.action" method="post"> 12 <h4 align="center">书籍查询</h4> 13 <table border="3" align="center"> 14 <tr> 15 <td>书名</td> 16 <td>作者</td> 17 <td>ISBN</td> 18 <td>出版社</td> 19 </tr> 20 <s:iterator value="#request.list" var="book"> 21 <tr> 22 <td><s:property value="#book.bookname" /></td> 23 <td><s:property value="#book.author" /></td> 24 <td><s:property value="#book.isbn" /></td> 25 <td><s:property value="#book.publish" /></td> 26 </tr> 27 <br /> 28 </s:iterator> 29 </table> 30 </s:form> 31 </body> 32 </html>
request取值:<s:iterator value="#request.list" var="book">
效果演示: