标签:end turn type sys 知识 support cti 封装 int
在学习Struts框架时,想复习一下所学知识,发现通过表达式封装表单数据到action,在action中无法获得数据,记录一下。
以下是各部分配置:
请求路径:
1 <package name="struts2" namespace="/" extends="json-default"> 2 <action name="action1" class="top.ionestu.action.ModeDrivenDemoAction"></action> 3 </package>
页面配置:
1 <form action="${pageContext.request.contextPath}/action1.action" method="post"> 2 bookName:<input type="text" name="book.bookName"><br> 3 price:<input type="text" name="book.price"><br> 4 bookSN:<input type="text" name="book.bookSN"><br> 5 <input type="submit" value="提交"> 6 </form>
Action类:
public class ModeDrivenDemoAction extends ActionSupport { private Book book; public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public String action1() throws Exception { System.out.println(book); return NONE; } }
在页面提交请求后,后台得到的数据是null,查了半天原来发现实体类忘记添加get set方法了,添加上之后
1 public class Book { 2 private String bookName; 3 private String price; 4 private String bookSN; 5 6 public String getBookName() { 7 return bookName; 8 } 9 10 public void setBookName(String bookName) { 11 this.bookName = bookName; 12 } 13 14 public String getPrice() { 15 return price; 16 } 17 18 public void setPrice(String price) { 19 this.price = price; 20 } 21 22 public String getBookSN() { 23 return bookSN; 24 } 25 26 public void setBookSN(String bookSN) { 27 this.bookSN = bookSN; 28 } 29 30 @Override 31 public String toString() { 32 return "Book{" + 33 "bookName=‘" + bookName + ‘\‘‘ + 34 ", price=‘" + price + ‘\‘‘ + 35 ", bookSN=‘" + bookSN + ‘\‘‘ + 36 ‘}‘; 37 } 38 }
数据获取正常了。
Struts2表达式封装Action获取不到表单数据的一次记录
标签:end turn type sys 知识 support cti 封装 int
原文地址:https://www.cnblogs.com/jizhiqi/p/12455194.html