<a> tag in html</a>

时间:2011-05-20 15:22:45

标签: javascript html jsp

我在JSP文件中有以下代码:

rs=stmt.executeQuery("select * from routemaster");
if(rs!=null){
   %>
   <table class="notebook" align="center">
   <tr class=row_title>
   <th class=row_title>RouteID</th>
   <th class=row_title>RouteCode</th>
   <th class=row_title>BusNo</th>
   <th class=row_title>InBoundTime</th>
   <th class=row_title>OutBoundtime</th>
   <th class=row_title>Location</th></tr>
   <%
   while(rs.next()){
        RouteID=rs.getString(1);
        RouteCode=rs.getString(2);
        InBoundtime=rs.getString(4);
        OutBoundtime=rs.getString(5);
        BusNo=rs.getString(6);
        Location=rs.getString(7);
        DisRow++;

       %><tr class= <%=(DisRow%2!=0)? "row_even" : "row_odd"%>>
       <td><%=RouteID%></td>
       <td><a onclick="sendInfo('<%=RouteCode%>') " ><%=RouteCode%></a></td>
       <td><%=BusNo%></td>
       <td><%=InBoundtime%></td>
       <td><%=OutBoundtime%></td>
       <td><%=Location%></td>
   </tr><%
   }
   %></table><% 
}

以下JS代码:

sendInfo(txt){
    var txt = window.opener.document.addbus0.RouteCode;
    txt.value = txtVal;
    window.close();
}

只要点击了与RouteCode的链接,就需要关闭该窗口,并且需要将所选的RouteCode存储在会话中。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

到目前为止,您需要将值从客户端发送到服务器。实现此目的的常规方法是让客户端单击链接或将表单提交到服务器端的URL,并将值作为请求参数发送到URL中或作为表单中的隐藏输入。

由于您使用的是链接,因此这是一个带链接的示例:

<a href="sendInfo.jsp?routeCode=<%=routeCode%>"><%=routeCode%></a>

然后在sendInfo.jsp中执行

<% 
  String routeCode = request.getParameter("routeCode");
  session.setAttribute("routeCode", routeCode);
%>
<script>
  window.opener.document.addbus0.RouteCode.value = '<%=routeCode%>';
  window.close();
</script>

对具体问题

无关,这种代码风格不是最佳做法。 Java代码属于Java类,而不属于JSP文件。 JSP文件应该只包含HTML,JSP标记和EL。另请参阅How to avoid Java code in JSP files?此外,Java coding conventions要求变量名称应以小写开头。仔细阅读它们。你还有很长的路要走,祝你好运。