我使用了struts2 spring和hibernate整合一个功能模块,但在提交表单时,struts2无法接收到表单传递过来的参数,这个问题已经困扰了我一天了,请高手指点。
表单如下:<body>
<s:form theme="simple" action="booksAction_addBook" method="get">
<table align="center" width="100%">
<tr>
<th colspan="2">
增加书箱
</th>
</tr>
<tr>
<td>
书名
</td>
<td>
<s:textfield name="books.bookName"></s:textfield>
</td>
</tr>
<tr>
<td>
库存
</td>
<td>
<s:textfield name="books.count"></s:textfield>
</td>
</tr>
<tr>
<td>
单价
</td>
<td>
<s:textfield name="books.price"></s:textfield>
</td>
</tr>
<tr>
<td>
目录
</td>
<td>
<%

CKEditorConfig settings = new CKEditorConfig();
settings.addConfigValue("width", "900px");
request.setAttribute("settings", settings);
%>

<ckeditor:editor config="<%=settings %>" editor="books.content"
basePath="ckeditor/"></ckeditor:editor>
</td>
</tr>
<tr>
<td>
描述
</td>
<td>

<textarea rows="10" cols="90" name="books.description"
id="books.description"></textarea>
</td>
</tr>
<tr>
<td>
时间
</td>
<td>
<s:textfield name="books.dt"></s:textfield>
</td>
</tr>
<tr>
<td clospan="2">
<s:submit value="添加" theme="simple"></s:submit>
</td>
</tr>
</table>
</s:form> <ckeditor:replace replace="books.description" config="${settings }"
basePath="ckeditor/"></ckeditor:replace>
</body>
Action如下:
public class BooksAction extends ActionSupport implements RequestAware {

private Books books;

public String addBook() throws Exception {
System.out.println("添加="+books);
return index();
}
}Books对象实体如下:
package com.yier.entity;import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;/**
 * Books entity. @author MyEclipse Persistence Tools
 */public class Books implements java.io.Serializable { // Fields /**
 * 
 */
private static final long serialVersionUID = 1L;
private long id;
private String bookName;
private long count;
private String price;
private String content;
private String description;
private Date dt; // Constructors /** default constructor */
public Books() {
} /** full constructor */
public Books(String bookname, long count, String price,
String content, String description, Date dt) {
this.bookName = bookname;
this.count = count;
this.price = price;
this.content = content;
this.description = description;
this.dt = dt;
} // Property accessors public long getId() {
return this.id;
} public void setId(long id) {
this.id = id;
} public String getBookName() {
return bookName;
} public void setBookName(String bookName) {
this.bookName = bookName;
} public long getCount() {
return count;
} public void setCount(long count) {
this.count = count;
} public String getPrice() {
return this.price;
} public void setPrice(String price) {
this.price = price;
} public String getContent() {
return this.content;
} public void setContent(String content) {
this.content = content;
} public String getDescription() {
return this.description;
} public void setDescription(String description) {
this.description = description;
} public Date getDt() {
return dt;
} public void setDt(Date dt) {
this.dt = dt;
} @Override
public String toString() {
// TODO Auto-generated method stub
return "id=" + id + ",bookName=" + bookName + ",count=" + count
+ ",price=" + price + ",content=" + content + ",description="
+ description + ",dt=" + dt;
}
}
当表单提交,控制台打印如下:
添加=id=0,bookName=null,count=0,price=22,content=null,description=null,dt=null
就是接收不到表单提交的信息,
郁闷啊。玩struts2两年了,这问题第一次遇到,请指教。

解决方案 »

  1.   

    Action如下:
    public class BooksAction extends ActionSupport implements RequestAware {private Books books;public String addBook() throws Exception {
    System.out.println("添加="+books);
    return index();
    }
    }action接收用户数据有三种方法,你定义了books,并没有写setter/getter方法,这是不行的,数据都是能过getter方法来注入的.或者你可以通过action实现modeldriven接口,写上private Books books=new Books();就行了,不要getter/setter方法.
      

  2.   

    Action中books对象没有set、get。
      

  3.   

    看看action总 books对象有没有get set 方法
    建议提交表单方式用post
      

  4.   

    实现modeldriven接口 不然表单中提交数据的name 必须是 book.xxx格式的
      

  5.   

    楼上也都说了
    你前台表单的name都是
    name="books.xxxxx"  这样的、
    而你action 一共就这几行代码且 books 这变量没有setter 和getter
    而你前台又对books做了实体.属性的赋值动作、
    所以你action 无法获取到内容、
    请设置
    private Books books;的setter 和getter
      

  6.   

    在项目中,我其实是加了getter和setter的,只是发贴的时候,不小心删除了。action中加了setter/getter,还是一样的问题啊!
    Action如下:
    public class BooksAction extends ActionSupport implements RequestAware {
    private IBooksDao booksDao;
    private Books books;
    private final int PAGESIZE = 5;
    private int pageNow = 1;
    private Map<String, Object> request = null; public void setPageNow(int pageNow) {
    this.pageNow = pageNow;
    } public void setBooks(Books books) {
    this.books = books;
    } public void setBooksDao(IBooksDao booksDao) {
    this.booksDao = booksDao;
    } /**
     * 添加
     * @return
     * @throws Exception
     */
    public String addBook() throws Exception {
    System.out.println("添加="+books);
    return index();
    }
    /**
     * 删除
     * 
     * @return
     * @throws Exception
     */
    public String deleteByID() throws Exception {
    this.booksDao.deleteByID((int) books.getId());
    return index();
    } /**
     * 更新预处理
     * 
     * @return
     * @throws Exception
     */
    public String preUpdateByID() throws Exception {
    Books b = this.booksDao.findBookById((int) books.getId());
    request.put("book", b);
    return "preUpdate";
    } /**
     * 更新
     * 
     * @return
     * @throws Exception
     */
    public String updateByID() throws Exception {
    System.out.println(books);
    //this.booksDao.updateByID(books);
    return index();
    } /**
     * 查看一本书的详细信息
     * 
     * @return
     * @throws Exception
     */
    public String showBooksDetail() throws Exception {
    Books b = this.booksDao.findBookById((int) books.getId());
    request.put("book", b);
    return "show";
    } /**
     * 主页
     * 
     * @return
     * @throws Exception
     */
    public String index() throws Exception {
    long count = this.booksDao.getCount();
    long pageCount = count % PAGESIZE == 0 ? count / PAGESIZE : count
    / PAGESIZE + 1;
    request.put("pageCount", pageCount);
    if (pageNow > pageCount || pageNow < 1) {
    pageNow = 1;
    }
    request.put("pageNow", pageNow); List<Books> books = this.booksDao.findBooks(pageNow, PAGESIZE);
    request.put("books", books);
    return "index";
    } public void setRequest(Map<String, Object> request) {
    this.request = request;
    }
    }
      

  7.   

    问题解决了,在Action中还必须为域模型,提供setter和getter,两个都必须要有,我以前一直以为只提供setter就可以了;但还是不明白,接收表单参数getter的作用是什么哈?请高手指点