我是JSP初学者,按一本书上的代码在我机器上运行,为什么总是不对呢?
有两个页面。一个是index.jsp,一个是showportal.jsp。在index.jsp中作选择之后,跳转到showportal.
index的代码如下:
<html>
<head>
<title>first page</title>
</head>
<body>
<form action="showportal.jsp" method="get">
<select name="portchoice">
<option>news</option>
<option>entertainment</option></select>
<input type="submit" value="Select"/>
</form>
</body>
</html>
showportal的代码如下:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head><title>second page. </title></head>
<c:choose>
<c:when test="${param.portchoice == 'news'}">
<body>
<h1>News</h1>
</body>
</c:when>
<c:when test="${param.portchoice=='entertainment'}">
<body>
<h1>Entertainment</h1>
</body>
</c:when>
<c:otherwise>
<body>
<h1>Otherwise.</h1>
</body>
</c:otherwise>
</c:choose>
</html>
运行时,为什么无论在index中选择的是new还是entertainment, showportal中显示的都是Otherwise呢?
有两个页面。一个是index.jsp,一个是showportal.jsp。在index.jsp中作选择之后,跳转到showportal.
index的代码如下:
<html>
<head>
<title>first page</title>
</head>
<body>
<form action="showportal.jsp" method="get">
<select name="portchoice">
<option>news</option>
<option>entertainment</option></select>
<input type="submit" value="Select"/>
</form>
</body>
</html>
showportal的代码如下:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head><title>second page. </title></head>
<c:choose>
<c:when test="${param.portchoice == 'news'}">
<body>
<h1>News</h1>
</body>
</c:when>
<c:when test="${param.portchoice=='entertainment'}">
<body>
<h1>Entertainment</h1>
</body>
</c:when>
<c:otherwise>
<body>
<h1>Otherwise.</h1>
</body>
</c:otherwise>
</c:choose>
</html>
运行时,为什么无论在index中选择的是new还是entertainment, showportal中显示的都是Otherwise呢?
String str = request.getParameter("portchoice");
if ("news".equals(str)) {
%>
<body>
<h1>News</h1>
</body><%
} else if ("entertainment".equals(str)) {
%>
<body>
<h1>Entertainment</h1>
</body>
<%
} else {
%>
<body>
<h1>Otherwise.</h1>
</body>
<%
}
%>
用这段代码来代替<c:choose>*</c:choose>块
测试结果看看