当前位置:编程学习 > JAVA >>

在jsp中传值给action的问题

<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;)]等
JSP struts2 ognl --------------------编程问答-------------------- value="%{#request.questionBefore.qcontent}"/>加上%就表示是字符串,和bean里的类型不一致,当然错了,改为
value="#request.questionBefore.qcontent"/>或者value="{#request.questionBefore.qcontent}"/> --------------------编程问答-------------------- 好像不是吧,我是先把它显示在tesxtfield里面改了的话就不能显示出来了
成这样了
本来是这样

然后点提交吧这里面的东西都放到question对象里去 --------------------编程问答-------------------- value="#request.questionBefore.qcontent"/>这样也不行? --------------------编程问答-------------------- 嗯 也不行 用%只是让里面的这个变成ognl表达式才能输出 是不是提交的时候textfield里的东西都是string类型的  question对象set里的其他类型的方法就不行 --------------------编程问答-------------------- 原来隐藏域改为<input type="hidden" name="question.id" value='<s:property value="#request.questionBefore.qcontent"/>'/> --------------------编程问答-------------------- 这个隐藏域我写错了,这个隐藏域是用来保存question对象里的id属性的,按你说的改了

<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.id" value="%{#request.questionBefore.id}"/> --------------------编程问答-------------------- 嗯  question 对象的id没问题了  question中list<Option>  options  有问题  

<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>


只有这条不不报错

<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.id

Error setting expression 'question.options[0].oright' with value '[Ljava.lang.String;@24164d75'
ognl.NoSuchPropertyException: java.lang.String.oright
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,