我想知道测试条件jstl If -
boolean cbtest1=interests[0]!=null;//cbtest1 is true
这是我的jsp代码 -
<td >interest1
<c:choose>
<c:when test="${cbtest1}">
<input type="checkbox" name="interest1" checked/>
</c:when>
<c:otherwise>
<input type="checkbox" name="interest1" />
</c:otherwise>
</c:choose>
</td>
但是当我渲染页面时,otherwise
块被执行了!
我也测试了以下 -
<c:when test="${interests[0]!=null}">
在这种情况下otherwise
也已执行。
当我使用negation
-
<c:when test="${interests[0]==null}">
或 -
<c:when test="${!cbtest1}">
上面两个案例when
已执行。我错了吗?
interests[0]
的值为"y"
,而cbtest
的值为true
。
附加了页面的屏幕截图,其中显示了cbtest和interest [o]的值,即interest1正在显示。
正如JBNIZT建议的那样 - 我做了以下更改,我在请求中设置了布尔数组而不是字符串数组.-
Servlet1.java
@WebServlet("/serv")
public class servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username").trim();
String password=request.getParameter("password").trim();
System.out.println("Hellow! "+username);
try{
Connection con=CommonUtil.getConnection();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from login");
int flag=0;
while(rs.next()){
if(rs.getString(1).trim().equals(username)&&rs.getString(2).trim().equals(password)){
flag=1;
HttpSession session=request.getSession();
session.setAttribute("loginId", rs.getString(3).trim());
ResultSet rs1=st.executeQuery("select * from details where loginid='"+ rs.getString(3).trim()+"'");
rs1.next();
String name=rs1.getString(1);
String address=rs1.getString(2);
String hobby=rs1.getString(4);
ResultSet rs2=st.executeQuery("select * from interest where loginid='"+session.getAttribute("loginId")+"'");
rs2.next();
String interest1=rs2.getString(2);
String interest2=rs2.getString(3);
String interest3=rs2.getString(4);
request.setAttribute("name", name);
request.setAttribute("address", address);
request.setAttribute("hobby", hobby);
boolean[] interest={(interest1!=null),(interest2!=null),(interest3!=null)};
request.setAttribute("interest", interest);
System.out.println(interest1+interest2+interest3);
System.out.println(name+address+hobby);
request.getRequestDispatcher("display.jsp").forward(request, response);
return;
}
}
if(flag==0){
request.setAttribute("message", "invalide login credentials!");
request.getRequestDispatcher("index.jsp").forward(request, response);
return;
}
}catch(Exception e){
System.out.println(e);
}
}
}
display.jsp
<%@ page language="java" contentType="text/html"%>
<%@page import="java.sql.Connection" import="java.util.Enumeration" import="java.sql.ResultSet"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hellow!!
<%
out.println(session.getAttribute("loginId"));
String name=(String)request.getAttribute("name");
String address=(String)request.getAttribute("address");
String hobby=(String)request.getAttribute("hobby");
boolean[] interests=(boolean[])request.getAttribute("interest");
out.println(name+"</br>address- "+address+"</br>hobby- "+hobby+"</br>Interest1- "+interests[0]+"</br>Interest2- "+interests[1]+"</br>Interest3- "+interests[2]+"</br>interests- "+interests);
%>
<form action="updateServ" method="post">
<table>
<tr >
<td >Name:</td>
<td ><input type="text" name="username" value="${name}"/></td>
</tr>
<tr>
<td> Address:</td>
<td ><input type="text" name="address" value="${address}"/></td>
</tr>
</table>
<table>
<tr >
<td >interest1
<c:choose>
<c:when test="${interests[0]}">
<input type="checkbox" name="interest1" checked/>
</c:when>
<c:otherwise>
<input type="checkbox" name="interest1" />
</c:otherwise>
</c:choose>
</td>
<td >interest2<input type="checkbox" name="interest2" /></td>
<td >interest3<input type="checkbox" name="interest3" /></td>
</tr>
<tr>
<td >Hobby1<input type="radio" value="hobby1" name="hobby"/></td>
<td >Hobby2<input type="radio" value="hobby2" name="hobby"/></td>
<td >Hobby3<input type="radio" value="hobby3" name="hobby"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Update"></td>
<td></td>
</tr>
</table>
</form>
这仍然无效。