在同一页面中存储选定元素值的位置?

时间:2013-03-06 04:55:44

标签: java jsp jquery servlets url-rewriting

我有 main.jsp ,其中包含4个jsp页面,即 company.jsp,software.jsp,location.jsp,dept.jsp
结束将是“提交”按钮,该按钮将被重定向到另一个jsp页面

注意:在company.jsp和Software.jsp中,只需单击一个单选按钮
           在location.jsp中,dept.jsp具有多个复选框选中的复选框
我想改进我的搜索 像这样:

enter image description here

我希望当在company.jsp中选择一个单选按钮时,将根据公司选择的软件从数据库中显示。当选择软件时,带有复选框的位置将根据数据库显示。然后,如果选择了位置,则将根据数据库显示depts等。

我尝试过使用jquery,其中选中的单选按钮被添加到url作为参数但是这是不好的做法,因为我正在通过window.location.href更改url。这是不好的做法 使用servlet或jquery的任何其他想法?

修改

main.jsp中

<div style="background-color:#F4F4F4"><b>Company:</b></div>
<div id="company"> <jsp:include page="company.jsp"></jsp:include> </div>
<div style="background-color:#F4F4F4"><b>Software:</b></div>
<div id="software">
<jsp:include page="software.jsp"></jsp:include> </div>
<div style="background-color:#F4F4F4"><b>Location:</b></div>
<div id="location">
<jsp:include page="my_location.jsp"></jsp:include> </div>


company.jsp

<script src="jquery-1.9.1.min.js"></script>
<script src="jquery-1.9.1.js"></script>
<script type="text/javascript">
function call(a)
{
    window.location.href="main.jsp?company="+a ;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","","");
ResultSet rs = null;
Statement st=null;
st=con.createStatement();
%>

<form name=company_form id=company_form>
<%
String name=request.getParameter("r");
if(name!=null){
//out.println("value in company.jsp:"+name);
}

%>
<br>
<%
try{
rs = st.executeQuery("select DISTINCT(COMPANY) from maindatabase");
int i=0; 
while(rs.next())
  {
%>
<input type="radio" name="r" onclick='call(this.value)' class='class_company' value="<%= rs.getString("COMPANY") %>"><%= rs.getString("COMPANY") %><br>
<%

//System.out.println("company count:"+i);
i++;
}


}
catch(SQLException e)
{ System.out.println(e.getMessage()); } 

%>
</form>

<script type="text/javascript">

function checkall()
{
        var prmstr = window.location.search.substr(1);
        //alert("string in company.jsp"+prmstr);
        var prmarr = prmstr.split ("&");
        var params = {};

        for ( var i = 0; i < prmarr.length; i++) {
            var tmparr = prmarr[i].split("=");
           // alert("tmparr[0] & tmparr[1] :"+tmparr[0]+" and "+tmparr[1] )
            params[tmparr[0]] = tmparr[1];
        }
         //alert("value to be selected in company.jsp:"+params.company);
        if(params.company=="company1")
            { 

            $('input:radio[value=company1]').attr('checked',true);  

            }
        else
            {
            if(params.company=="company2")
            { 

                $('input:radio[value=company2]').attr('checked',true); 
            }
            else
                {
                if(params.company=="company3")
                    {

                    $('input:radio[value=company3]').attr('checked',true);
                   }
                }       
            }

}
</script>
<script>
window.onload=checkall() ; 
</script>


software.jsp

<script type="text/javascript">
function stripWhiteSpace(arg){
       if(arg.replace(/^\s+/,'').replace(/\s+$/,'') == ""){
          return true;
       }
    }
function AddUrlParameter(a, b, c)
{
    if (stripWhiteSpace(b))
{
 alert("Parameter name should not be empty.");
 return a;
 } 
    if (stripWhiteSpace(c))
{
  alert("Parameter value should not be empty.");
  return a;
 }
if (a.indexOf("?") == -1)
{
  return a + "?" + b + "=" + c;
}
var d = a.split("?");
if (d.length >= 2)
{

      if (stripWhiteSpace(b))
    // if (d[1].trim() == "")
  {
      return d[0] + "?" + b + "=" + c;
  }
  var e = d[1].split(/[&;]/g);
  for (var f = 0; f < e.length; f++)
  {
    var g = e[f]; var h = g.split("=");
    if (h.length >= 2)
    {
      if (h[0] == b)
      {
           // alert("a :"+a);
            //  alert("b :"+b);
            //alert("c :"+c);
            /*
            if (stripWhiteSpace(b))
          {
            alert("Parameter name should not be empty.");
            return a ;
           }
            if (stripWhiteSpace(c))
          {
            alert("Parameter value should not be empty.");
            return a; 
          }
          var d = "";
          var e = false;
          var f = false;
          if (a.indexOf("?") == -1)
          {
             alert("No Url Parameters Found!");
             return a;
          }
          var g = a.split("?");
          if (g.length >= 2)
          {
            d = d + g[0] + "?";
            var h = g[1].split(/[&;]/g);
            for (var i = 0; i < h.length; i++)
            {
              var j = h[i];
              var k = j.split("=");
              if (k.length >= 2)
              {
                if (k[0] == b)
                {
                 f = true; k[1] = c;
                 d = d + b + "=" + c + "&";
                 }
                else 
                {
                    d = d + j + "&";
                }
                e = true;
              }
             }
            if (f == false)
            {
              alert("Requested query string not found to remove");
              return a;
             }
            if (e == true)
            {
              d = d.slice(0, d.length - 1);
            }
            return d; 
            }

          */
          alert("Url Parameter with provided name already exists! Try Updating that Url Parameter.");

          //UpdateUrlParameter(a, b, c);
          //return a;
       }
    }
   }
  return a + "&" + b + "=" + c;
  }
} 

</script>
<script type="text/javascript">
/*
function callsoft(a) {

    alert("hi");

    var id=document.getElementById("software");
    alert("id :"+id+" value"+a.value);

var $soft = $('input[name=soft]:checked'); 
      var softvalue = $soft.val();
      var id = $soft.attr('id');

      alert("radio clicked id:"+id+" and value: "+softvalue);

      var prmstr = window.location.search.substr(1);
        var prmarr = prmstr.split ("&");
        var params = {};

        for ( var i = 0; i < prmarr.length; i++) {
            var tmparr = prmarr[i].split("=");
              params[tmparr[0]] = tmparr[1];
        }
            var company=params.company;

    var companyurl="main.jsp?company="+company;
    alert(companyurl);

    var new_url = AddUrlParameter(companyurl,id,softvalue);

//var new_url = AddUrlParameter(window.location.href,id,softvalue);
window.location.replace(new_url);

*/


$(document).ready(function(){

    $('.class_sofware').click(function() {


        var $soft = $('input[name=soft]:checked'); 
              var softvalue = $soft.val();
              var id = $soft.attr('id');

              alert("radio clicked id:"+id+" and value: "+softvalue);

              var prmstr = window.location.search.substr(1);
            var prmarr = prmstr.split ("&");
            var params = {};

            for ( var i = 0; i < prmarr.length; i++) {
                var tmparr = prmarr[i].split("=");
                  params[tmparr[0]] = tmparr[1];
            }

            var company=params.company;

            var companyurl="main.jsp?company="+company;

            var new_url = AddUrlParameter(companyurl,id,softvalue);

       //var new_url = AddUrlParameter(window.location.href,id,softvalue);
       window.location.replace(new_url);


    });
});


</script>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div id="i_all_sw">
<%
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","","");
ResultSet rs = null;
Statement st=null;
st=con.createStatement();
%>

<%
String S_COMPANY=request.getParameter("company");
if(S_COMPANY!=null){
System.out.println(S_COMPANY);
}
%>
<%
try{
rs = st.executeQuery("select DISTINCT(SW_NAME) from maindatabase where COMPANY='"+S_COMPANY+"'");

int i=0; 
while(rs.next())
  {
%>
<input type="radio" name="soft" id="software" onclick="callsoft(this);" class="class_sofware" value="<%= rs.getString("SW_NAME") %>"><%= rs.getString("SW_NAME") %><br>
<%

//System.out.println("company count:"+i);
i++;
}


}
catch(SQLException e)
{ System.out.println(e.getMessage()); } 

%>
</div>
<script type="text/javascript">

function checkall()
{
        var prmstr = window.location.search.substr(1);
        //alert("string in software.jsp"+prmstr);
        var prmarr = prmstr.split ("&");
        var params = {};

        for ( var i = 0; i < prmarr.length; i++) {
            var tmparr = prmarr[i].split("=");
              params[tmparr[0]] = tmparr[1];
        }
        //alert("params.software value to be selected in software.jsp:"+params.software);
        $('input:radio[value='+params.software+']').attr('checked',true);  


}
</script>
<script>
window.onload=checkall();  
</script>

1 个答案:

答案 0 :(得分:1)

main.jsp执行以下跟踪

<form name="myForm" id="myFormId"
 action="/myServlet" method="POST">

 1. include company.jsp  
 2. include software.jsp
 3. include location.jsp
 4. include dept.jsp

<input type="submit" value="submit">
</form>

注意:单选按钮的名称在company.jspSoftware.jsp

中应相同

当out将呈现给浏览器时,将只有一个表单元素和内部表单元素,根据包含的JSP,将有单选按钮和复选框。