<s:form action="updateQuestion" theme="simple">
<table>
<tr>
<td>试题标题</td>
<td><s:textfield name="question.qcontent" value="%{#request.questionBefore.qcontent}"></s:textfield>
<s:hidden name="question.id" value="%{#request.questionBefore.qcontent}"/>
</td>
</tr>
<s:iterator value="#request.questionBefore.options" id="op" status="st">
<tr>
<td>选项<s:property value="choice"/></td>
<td>
<s:hidden name="question.options[%{#st.getIndex()}].id" value="%{id}"/>
<s:textfield name="question.options[%{#st.getIndex()}]" value="%{ocontent}"></s:textfield>
<s:hidden name="question.options[%{#st.getIndex()}].choice" value="%{choice}"/>
<s:checkbox name="question.options[%{#st.getIndex()}].oright"/>
</td>
</tr>
</s:iterator>
<tr>
<s:submit value="修改"/>
</tr>
</table>
</s:form>这是做一个更新的操作,先把原来的Question的所有内容显示出来,在改需要更改的地方,再提交调用hibernate session的update方法来实现,但是Question里的内容赋值是,ognl调用对象里面的set方法时类型不对,通过value赋的值都是String类型,怎么办啊public class Question {
private int id;//主键
private String qcontent;//试题题目
private List<Option> options;//一堆多,持有Option的集合
public int getId() {
return id;
}
public String getQcontent() {
return qcontent;
}
public List<Option> getOptions() {
return options;
}
public void setId(int id) {
this.id = id;
}
public void setQcontent(String qcontent) {
this.qcontent = qcontent;
}
public void setOptions(List<Option> options) {
this.options = options;
}
}public class Option {
private int id; //主键
private String choice;//选项 A,B,C,D
private String ocontent;//选项内容
private boolean oright;//是否是正确答案
public int getId() {
return id;
}
public String getOcontent() {
return ocontent;
} public void setId(int id) {
this.id = id;
}
public void setOcontent(String ocontent) {
this.ocontent = ocontent;
}
public boolean isOright() {
return oright;
}
public void setOright(boolean oright) {
this.oright = oright;
}
public String getChoice() {
return choice;
}
public void setChoice(String choice) {
this.choice = choice;
}
}下面是我在Action里的测试:
public String modify(){
//根据试题标题先查找到试题的所有内容,并显示到textfield在提交调用add方法
System.out.println(question.getId());
System.out.println(question.getQcontent());
for(Option o:question.getOptions()){
System.out.println(o.getId());
System.out.println(o.getChoice());
System.out.println(o.getOcontent());
System.out.println(o.isOright());
}
他会提示WARN OgnlValueStack:60 - Error setting expression 'question.id' with value '[Ljava.lang.String;@6c267f18'
ognl.MethodFailedException: Method "setId" failed for object com.zw.po.Question@4cf8f332 [java.lang.NoSuchMethodException: com.zw.po.Question.setId([Ljava.lang.String;)]等
JSPstruts2ognl
<table>
<tr>
<td>试题标题</td>
<td><s:textfield name="question.qcontent" value="%{#request.questionBefore.qcontent}"></s:textfield>
<s:hidden name="question.id" value="%{#request.questionBefore.qcontent}"/>
</td>
</tr>
<s:iterator value="#request.questionBefore.options" id="op" status="st">
<tr>
<td>选项<s:property value="choice"/></td>
<td>
<s:hidden name="question.options[%{#st.getIndex()}].id" value="%{id}"/>
<s:textfield name="question.options[%{#st.getIndex()}]" value="%{ocontent}"></s:textfield>
<s:hidden name="question.options[%{#st.getIndex()}].choice" value="%{choice}"/>
<s:checkbox name="question.options[%{#st.getIndex()}].oright"/>
</td>
</tr>
</s:iterator>
<tr>
<s:submit value="修改"/>
</tr>
</table>
</s:form>这是做一个更新的操作,先把原来的Question的所有内容显示出来,在改需要更改的地方,再提交调用hibernate session的update方法来实现,但是Question里的内容赋值是,ognl调用对象里面的set方法时类型不对,通过value赋的值都是String类型,怎么办啊public class Question {
private int id;//主键
private String qcontent;//试题题目
private List<Option> options;//一堆多,持有Option的集合
public int getId() {
return id;
}
public String getQcontent() {
return qcontent;
}
public List<Option> getOptions() {
return options;
}
public void setId(int id) {
this.id = id;
}
public void setQcontent(String qcontent) {
this.qcontent = qcontent;
}
public void setOptions(List<Option> options) {
this.options = options;
}
}public class Option {
private int id; //主键
private String choice;//选项 A,B,C,D
private String ocontent;//选项内容
private boolean oright;//是否是正确答案
public int getId() {
return id;
}
public String getOcontent() {
return ocontent;
} public void setId(int id) {
this.id = id;
}
public void setOcontent(String ocontent) {
this.ocontent = ocontent;
}
public boolean isOright() {
return oright;
}
public void setOright(boolean oright) {
this.oright = oright;
}
public String getChoice() {
return choice;
}
public void setChoice(String choice) {
this.choice = choice;
}
}下面是我在Action里的测试:
public String modify(){
//根据试题标题先查找到试题的所有内容,并显示到textfield在提交调用add方法
System.out.println(question.getId());
System.out.println(question.getQcontent());
for(Option o:question.getOptions()){
System.out.println(o.getId());
System.out.println(o.getChoice());
System.out.println(o.getOcontent());
System.out.println(o.isOright());
}
他会提示WARN OgnlValueStack:60 - Error setting expression 'question.id' with value '[Ljava.lang.String;@6c267f18'
ognl.MethodFailedException: Method "setId" failed for object com.zw.po.Question@4cf8f332 [java.lang.NoSuchMethodException: com.zw.po.Question.setId([Ljava.lang.String;)]等
JSPstruts2ognl
value="#request.questionBefore.qcontent"/>或者value="{#request.questionBefore.qcontent}"/>
成这样了
本来是这样然后点提交吧这里面的东西都放到question对象里去
<s:hidden name="question.id" value='<s:property value="#request.questionBefore.id"/>'/>
还是显示ognl.MethodFailedException: Method "setId" failed for object com.zw.po.Question@7926b165 [java.lang.NoSuchMethodException: com.zw.po.Question.setId([Ljava.lang.String;)]
<s:hidden name="question.options[%{#st.getIndex()}].id" value="%{id}"/>
<s:textfield name="question.options[%{#st.getIndex()}]" value="%{ocontent}"></s:textfield>
<s:hidden name="question.options[%{#st.getIndex()}].choice" value="%{choice}"/>
<s:checkbox name="question.options[%{#st.getIndex()}].oright"/>
</td>只有这条不不报错<s:textfield name="question.options[%{#st.getIndex()}]" value="%{ocontent}"></s:textfield>
其他都报错
Error setting expression 'question.options[0].choice' with value '[Ljava.lang.String;@2994363b'
ognl.NoSuchPropertyException: java.lang.String.choice
Error setting expression 'question.options[0].id' with value '[Ljava.lang.String;@7ffe9999'
ognl.NoSuchPropertyException: java.lang.String.idError setting expression 'question.options[0].oright' with value '[Ljava.lang.String;@24164d75'
ognl.NoSuchPropertyException: java.lang.String.oright