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

struts中的问题?

JSP页面中

<logic:iterate id="exam" name="stu_exam_list" type="com.aftvc.exam.entity.Exam" indexId="s" >
  <h3>第<%= s+1 %> 题</h3>
 <table width="600" border="0" cellpadding="0" cellspacing="0">
  <tr>
  <td colspan="2" align="left"><span class="conList"><strong>${exam.sq_question }<input type="hidden" name="sq_id" ></strong></span></td>
  </tr>
  <tr>
  <td width="31" align="right">(A)</td>
  <td width="559" align="left"> ${exam.sq_question_A}</td>
  </tr>
  <tr>
  <td align="right">(B)</td>
  <td align="left"> ${exam.sq_question_B}</td>
  </tr>
  <tr>
  <td align="right">(C)</td>
  <td align="left"> ${exam.sq_question_C}</td>
  </tr>
  <tr>
  <td align="right">(D)</td>
  <td align="left"> ${exam.sq_question_D}</td>
  </tr>
  </table>
<table width="760" border="0">
  <tr>
  <td><div align="left"><strong><span class="conList">选择答案:</span>   <span class="conList">A</span>
  <input type="radio" name="${exam.sq_id}" value="radiobutton" />  
  <span class="conList"> B</span>
  <input type="radio" name="${exam.sq_id}" value="radiobutton" /> 
  <span class="conList"> C</span>
  <input type="radio" name="${exam.sq_id}" value="radiobutton" /> 
  <span class="conList">
  D</span>
  <input type="radio" name="${exam.sq_id}" value="radiobutton" />
  </strong></div></td>
  </tr>
  </table>
Action中
读取回答过的问题内容,答案
public ActionForward Stu_exam_answer(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException {
ExamForm examForm = (ExamForm) form;// TODO Auto-generated method stub
String[] answerlist= request.getParameterValues("sq_id");
System.out.println(answerlist.length);
if(answerlist.length<20){
out.print("<script>confirm('还有未选择的试题,确定交卷么?')</script>");
}
else{
request.setAttribute("stu_exam_answerlist", answerlist);
return mapping.findForward("stu_exam_answer");
}
return mapping.findForward("stu_exam_answer");
}在action中的System.out.println(answerlist.length);
出现错误,我找不出来!提示内容为:javax.servlet.ServletException: java.lang.NullPointerException
--------------------编程问答-------------------- ${exam.sq_id}是变的吧
也就是你页面根本没有名称为sq_id的参数
所以下面
String[] answerlist= request.getParameterValues("sq_id");
System.out.println(answerlist.length);//answerlist是null,所以会报空指针异常 --------------------编程问答-------------------- 把sq_id放在<input type="hidden" name="sq_id" >不行吗? --------------------编程问答-------------------- 是post提交吗? 
<form method="post"> --------------------编程问答-------------------- 还有,JSP提交之前,alert 一下 sq_id 的值 --------------------编程问答-------------------- <INPUT style="border-width: 0px;" id="ctl00_Content_ImageButton1" 
      class="Pointer" name="ctl00$Content$ImageButton1" src="images/stu_exam/dajuan.gif" 
      type="image" onClick="viewTest();return false;">
function viewTest()
{
    if(confirm("您现在是预览试卷,只能看到一部分试题!\n马上进入正式考试吗?"))
    {
              location.href = "http://localhost:8080/ExamOnline/exam.do?operate=Stu_exam_answer"
    }

    return false;
}提交 --------------------编程问答-------------------- 设个断点。。debug一步一步的调试 看看那步抛的空指针,,就能找到错误了。。。高效率的做法就是debug单步调式【js更应该debug,不然更难找】,,通过system.out或者alert去打印都是低效率。。。不提倡。。。 --------------------编程问答-------------------- 两种解决方法

一种是改为post方式提交;

另一种是仍使用你的提交方式,但是要先用js,取出sq_id的数组,拼接成字符串,然后加到你的url中传到后台。
location.href = "http://localhost:8080/ExamOnline/exam.do?operate=Stu_exam_answer" 
  + "&sq_ids=" + 拼接的字符串;
后台使用 String s = getParameter("sq_ids");  来接收。

你这种get方式,只能传递字符串,后台只能用getParameter来接收,不能传递对象。 --------------------编程问答-------------------- 你这是get方式通过url提交,就传了一个method,其他的什么也没传到后台
你应该将最外面的table用form包起来
<form name="form1" action="ExamOnline/exam.do?operate=Stu_exam_answer" >
<table .......>
..........
</table>
</form>
然后js里面就是
function viewTest()
{
  if(confirm("您现在是预览试卷,只能看到一部分试题!\n马上进入正式考试吗?"))
  {
   document.form1.submit();
  }

  return false;
}

还有
out.print("<script>confirm('还有未选择的试题,确定交卷么?')</script>");
这句话编译器没提示错误吗  --------------------编程问答--------------------
引用 8 楼 wangxf_8341 的回复:
你这是get方式通过url提交,就传了一个method,其他的什么也没传到后台
你应该将最外面的table用form包起来
<form name="form1" action="ExamOnline/exam.do?operate=Stu_exam_answer" >
<table .......>
..........
</table>
</form>
然后js里面就是
func……
没有提示,是空指针 --------------------编程问答--------------------
引用 7 楼 wolf863292 的回复:
两种解决方法

一种是改为post方式提交;

另一种是仍使用你的提交方式,但是要先用js,取出sq_id的数组,拼接成字符串,然后加到你的url中传到后台。
location.href = "http://localhost:8080/ExamOnline/exam.do?operate=Stu_exam_answer" 
  + "&sq_ids=" + 拼接的字符串;
……
要先用js,取出sq_id的数组,拼接成字符串?方法写一下,谢谢! --------------------编程问答-------------------- 拼接字符串的例子。没有写注释,应该能看懂。


<script>
function test(){

var str = "";

var obj = document.getElementsByTagName("input");

for(var i=0; i<obj.length; i++){
if(obj[i].type=="text" && obj[i].name=="a"){
str += obj[i].value + "-";
}
}

if(str.indexOf("-") > 0){
str = str.substring(0, str.length-1);
}
alert(str);
}
</script>

<body>
<input type="text" name="a" value="1"><br/>
<input type="text" name="a" value="2"><br/>
<input type="text" name="a" value="3"><br/>

<input type="button" onclick="test()">
</body>


不过还是建议使用post方式提交。 --------------------编程问答-------------------- js可以改改



<script>
function test(){

var str = "";

var obj = document.getElementsByName("a");

for(var i=0; i<obj.length; i++){
str += obj[i].value + "-";
}

if(str.indexOf("-") > 0){
str = str.substring(0, str.length-1);
}
alert(str);
}
</script>



--------------------编程问答-------------------- --------------------编程问答-------------------- 顶你哦。加油
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,