我对Struts 2非常熟悉......自从3天以来我一直陷入一个非常奇怪的问题
我的动作类中有很多属性;一些是整数,其他是字符串。我知道我已经为我的所有属性创建了两个getter / setter
showcase扩展了struts-default和json-default,因为我需要类来渲染一个json表,我正在使用 struts2-jquery gridtag ....没有我的属性被打印exept sord,sdix,page ..由标签定义的那些
我的动作类
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import amz.votrerepas.dao.CategorieDao;
import amz.votrerepas.dao.CategorieDaoImplementation;
import amz.votrerepas.dao.ProduitDao;
import amz.votrerepas.dao.ProduitDaoImplementation;
import amz.votrerepas.models.Produit;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
@ParentPackage(value = "showcase")
public class ProduitAction extends ActionSupport implements Preparable{
private static final long serialVersionUID = 947577563329037436L;
private List<Produit> produits;
private String searchOper = "asc"; // Search operator
private String sord; // sorting order - asc or desc
private String sidx; // get index row - i.e. user click to sort.
private String searchField; // Search Field
private String searchString; // The Search String
private String oper;
private Integer rows = 0;
private Integer page = 0;
private Integer total = 0;
private Integer records;
private String myeditOptions;
@Override
public void prepare() throws Exception {
CategorieDao catdao = new CategorieDaoImplementation();
Map<Long, String> listEditOptions = catdao.getAllCategories();
List<String> arraywithresulttupels = new ArrayList<String>();
for (Long key : listEditOptions.keySet()) {
arraywithresulttupels.add(""+key+":"+listEditOptions.get(key));
}
Collections.sort(arraywithresulttupels);
myeditOptions = "{value:'" + StringUtils.join(arraywithresulttupels, ";")
+ "'}";
}
@Override
public String execute() throws Exception {
ProduitDao dao = new ProduitDaoImplementation();
produits = dao.getallProducts();
return SUCCESS;
}
public String getJSON() throws Exception {
return execute();
}
public List<Produit> getProduits() {
return produits;
}
public void setProduits(List<Produit> produits) {
this.produits = produits;
}
public String getSearchOper() {
return searchOper;
}
public void setSearchOper(String searchOper) {
this.searchOper = searchOper;
}
public String getSord() {
return sord;
}
public void setSord(String sord) {
this.sord = sord;
}
public String getSidx() {
return sidx;
}
public void setSidx(String sidx) {
this.sidx = sidx;
}
public String getSearchField() {
return searchField;
}
public void setSearchField(String searchField) {
this.searchField = searchField;
}
public String getSearchString() {
return searchString;
}
public void setSearchString(String searchString) {
this.searchString = searchString;
}
public String getOper() {
return oper;
}
public void setOper(String oper) {
this.oper = oper;
}
public Integer getRows() {
return rows;
}
public void setRows(Integer rows) {
this.rows = rows;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public Integer getRecords() {
return records;
}
public void setRecords(Integer records) {
this.records = records;
}
public String getMyeditOptions() {
return myeditOptions;
}
public void setMyeditOptions(String myeditOptions) {
this.myeditOptions = myeditOptions;
}
}
我的JSP页面中的SomeWhere
<s:property value="%{myeditOptions}"/>
<s:property value="%{page}"/>
Struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<!-- ******************************** Comptes Actions ******************************** -->
<package name="default" extends="struts-default" namespace="/auth">
<action name="authenticate" class="amz.votrerepas.actions.Authenticate" method="execute">
<result name="success" type="redirectAction">
<param name="actionName">indexProduits</param>
<param name="namespace">/products</param>
</result>
<result name="error">/pages/welcome.jsp</result>
</action>
</package>
<!-- ******************************** Produits Actions ******************************** -->
<package name="categories" extends="struts-default" namespace="/products">
<action name="indexProduits" class="amz.votrerepas.actions.CategorieAction" method="execute">
<result name="success">/pages/admin/products.jsp</result>
</action>
<action name="edit-categorie-grid-entry" class="amz.votrerepas.actions.CategorieEditAction" method="execute">
<result name="success"> /pages/admin/products.jsp </result>
<result name="input"> /pages/admin/products.jsp </result>
</action>
<action name="edit-produit-grid-entry" class="amz.votrerepas.actions.ProduitEditAction" method="execute">
<result name="success"> /pages/admin/products.jsp </result>
<result name="input"> /pages/admin/products.jsp </result>
</action>
</package>
<!-- ******************************** Json Actions ******************************** -->
<package name="showcase" extends="struts-default,json-default" namespace="/jquery">
<action name="jsontableCats" class="amz.votrerepas.actions.CategorieAction" method="getJSON">
<result name="success" type="json" />
</action>
<action name="jsontableProds" class="amz.votrerepas.actions.ProduitAction" method="getJSON">
<result name="success" type="json" />
</action>
</package>
</struts>
我的自定义文件夹
antlr-2.7.6 antlr-2.7.7 asm-3.3.1 asm-3.3 asm-commons-3.3 asm-tree-3.3 cglib-2.2.2 codegen-0.5.9 commonj.sdo-2.1.1.v201112051852 commons-collections-3.1 commons-fileupload-1.2.2 commons-io-2.0.1 commons-lang-2.6 commons-lang3-3.1 dom4j-1.6.1 ecj-3.7.2 eclipselink-2.4.0 freemarker-2.3.19 guava-13.0.1 hibernate3 hibernate-commons-annotations-4.0.1.Final hibernate-core-4.1.7.Final hibernate-entitymanager-4.1.7.Final hibernate-jpa-2.0-api-1.0.0.Final hibernate-validator-4.3.0.Final javassist-3.11.0.GA javassist-3.12.0.GA javassist-3.15.0-GA javax.inject-1 javax.persistence-2.0.4.v201112161009 jboss-logging-3.1.0.GA jboss-transaction-api_1.1_spec-1.0.0.Final joda-time-1.6 json-lib-2.3-jdk15 jsr305-1.3.9 jta-1.1 log4j-1.2.16 mysema-commons-lang-0.2.4 mysql-connector-java-5.1.25-bin ognl-3.0.6 querydsl-apt-3.1.1 querydsl-codegen-3.1.1 querydsl-core-3.1.1 querydsl-jpa-3.1.1 querydsl-jpa-3.1.1-apt querydsl-jpa-3.1.1-javadoc querydsl-sql-3.1.1 slf4j-api-1.6.1 slf4j-log4j12-1.6.1 struts2-convention-plugin-2.3.14 struts2-core-2.3.14 struts2-jquery-grid-plugin-3.5.1 struts2-jquery-plugin-3.5.1 struts2-json-plugin-2.3.14 validation-api-1.0.0.GA xwork-core-2.3.14
显示页面的值,但myeditOptions的值不是..... Pleeeease帮助
答案 0 :(得分:0)
@ Lord-zed我看到你只在prepare()方法上填充myEditOptions。当“执行”动作被击中时,它不再被填充。因此,您必须在execute()中再次填充它,或者在prepare操作的jsp中放入一个隐藏字段,以将其发布回“执行”操作。我建议在execute()中重新填充它。
答案 1 :(得分:-1)
没有必要对getter和setter进行类型转换, 您应该创建不同的getter和setter以及DAO文件。