package com.web.main;import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;public class HttpUnitSelect { public static void main(String[] args) {
new HttpUnitSelect().changeSelectValue();
}
    public void changeSelectValue() {        try {
            WebConversation wc = new WebConversation();            // 如果我把index.jsp的值改为index.html,这个程序是工作正常的。
            WebRequest request = new PostMethodWebRequest(
                    "http://localhost:8080/login/index.jsp");
            WebResponse response = wc.getResponse(request);
            WebForm form = response.getForms()[0];            // 这个是想改变下拉框的值,虽然返回值(b)为值,但是下拉框的值还是为null.
            boolean b = form
                    .getScriptableObject()
                    .doEvent(
                            "javascript_:document.getElementById('aselect1').selectedIndex=2;");            form.setParameter("username", "1");
            form.setParameter("password", "1");
            System.out.println(form.getParameterValue("aselect1"));
            // form.setParameter("aselect1", "a"); //如果把这句的注释去掉就会报错
            for (String a : form.getParameterNames()) {
                System.out.println(a + "==" + form.getParameterValue(a));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
//下面是index.jsp的内容 
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/ecmascript">
    function chkSubmit() {
        if(document.getElementById("username1").value == "1" && document.getElementById("password1").value == "1") {
        document.getElementById("myform").action="welcome.jsp";
        document.getElementById("myform").submit();
        return true;
} else {
return false;
}
    }function changeSelect() {
    var obj = document.getElementById("aselect1");
    obj.selectedIndex=2;
}</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="myform"  method="post">
  <select name="aselect1" id="aselect1">
    <option value="a" id="aaa">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
  </select>
  <input type="text" name="username"   id="username1"  onclick="changeSelect();"/>
  <input type="password" name="password"  id="password1" />
  <input type="submit" onclick="javascript:chkSubmit();" value="button" name="button"  id="button"/>
</form>
<div id="welcome1">welcome1 to here</div>
</body>
</html>请各位高手帮助.

解决方案 »

  1.   

    自己顶,CSDN没有人可以解决吗?
      

  2.   

    谢谢,我感觉是junit对xhtml解析支持的不太好.
      

  3.   

    <select name="aselect1" id="aselect1">
    名字不要相同试试看
      

  4.   

    先用response.getText()把response的内容都打出来看看aselect1到底存在否。
      

  5.   

    response.getText() 输出的内容:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script type="text/ecmascript">
    function chkSubmit() {
    if(document.getElementById("username1").value == "1" && document.getElementById("password1").value == "1") {
    document.getElementById("myform").action="welcome.jsp";
    document.getElementById("myform").submit();
    return true;
    } else {
    return false;
    }
    }function changeSelect() {
    var obj = document.getElementById("aselect1");
    obj.selectedIndex=2;
    }</script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form id="myform"  method="post">
      <select name="aselect1" id="aselect1">
        <option value="a" id="aaa">a</option>
        <option value="b">b</option>
        <option value="c">c</option>
      </select>
      <input type="text" name="username"   id="username1"  onclick="changeSelect();"/>
      <input type="password" name="password"  id="password1" />
      <input type="submit" onclick="javascript:chkSubmit();" value="button" name="button"  id="button"/>
    </form>
    <div id="welcome1">welcome1 to here</div>
    </body>
    </html>个人感觉是httpunit不行。
      

  6.   

    <input type="submit" onclick="javascript:chkSubmit();" value="button" name="button"  id="button"/>
    你犯了个严重的错误:应该: type="button"  
      

  7.   

    除非用ONSUBMIT()事件
    你那JAVA不知道用了什么框架·
    QQ:357794271
      

  8.   

    仔细去看看ONSUBMIT()验证和FORM.SUBMIT()是怎么用的吧,而且有什么不同!
    我不知道你的JAVA程序是不是正确,但是 JS这样写一定是错的!
      

  9.   

    javascript_:document.getElementById('aselect1').selectedIndex=2;

    javascript:document.getElementById('aselect1').selectedIndex=2;
      

  10.   

    其实这个按钮根本没有用!请注意这句话结果返回的是true: boolean b = form
                        .getScriptableObject()
                        .doEvent(
                                "javascript_:document.getElementById('aselect1').selectedIndex=2;");
    如果我把目前的页面保存成html而不是jsp,代码完全没有问题.
      

  11.   

    找到原因了,把这个去掉就可以了,xmlns="http://www.w3.org/1999/xhtml"
    但还有问题,我是访问别人的网站,我总不能够把别人网站上面的东西去掉吧?
      

  12.   

     if(document.getElementById("username1").value == "1" && document.getElementById("password1").value == "1") {
            document.getElementById("myform").action="welcome.jsp";
            document.getElementById("myform").submit();
    this.myform.action = "welcome.jsp";
    this.myform.submit()
      

  13.   

    httpUnit在名字空间处理上可能存在缺陷,其实httpUnit在很多地方存在问题,个人感觉,目前还不够完善