读书人

struts2 没法接收参数

发布时间: 2012-08-01 17:53:40 作者: rapoo

struts2 无法接收参数
我使用了struts2 spring和hibernate整合一个功能模块,但在提交表单时,struts2无法接收到表单传递过来的参数,这个问题已经困扰了我一天了,请高手指点。。。。
表单如下:[size=16px][/size]<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两年了,这问题第一次遇到,请指教。。。。

[解决办法]
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方法.
[解决办法]
Action中books对象没有set、get。
[解决办法]
看看action总 books对象有没有get set 方法
建议提交表单方式用post
[解决办法]
楼上也都说了
你前台表单的name都是
name="books.xxxxx" 这样的、
而你action 一共就这几行代码

且 books 这变量没有setter 和getter
而你前台又对books做了实体.属性的赋值动作、
所以你action 无法获取到内容、
请设置
private Books books;

的setter 和getter


读书人网 >J2EE开发

热点推荐