SpringMVC—仅当传递了请求参数时才在JSP中显示值
我想创建一个JSP页面,用户可以从下拉列表中选择一个值,提交它并在同一页面上查看结果。我不知道怎样才能做到这一点。 以下是JSP页面:SpringMVC—仅当传递了请求参数时才在JSP中显示值,jsp,spring-mvc,Jsp,Spring Mvc,我想创建一个JSP页面,用户可以从下拉列表中选择一个值,提交它并在同一页面上查看结果。我不知道怎样才能做到这一点。 以下是JSP页面: <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Station choosing view</title>
</head>
<body>
<h1>Select the station</h1>
<form:form commandName="station" action="archive.htm" method="get" >
<form:select path="id">
<form:options items="${stations}" itemValue="id" itemLabel="localisation"></form:options>
</form:select>
<p><form:button>Submit</form:button></p>
</form:form>
<c:if test="${foundStation.id!=null}">
<p>Station code is ${foundStation.code}</p>
</c:if>
</body>
</html>
如果id为,如何避免打印foundStation的内容
访问页面时未传递参数
这是我的生日礼物。如果foundStation
为null
,则代码将抛出NullPointerException
。因此,无法访问id
属性
<c:if test="${foundStation.id!=null}">
这并没有改变任何事情。我相信您得到的错误与JSP中的
标记问题无关。试试我将发布的更新代码。
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
<c:if test="${foundStation.id!=null}">
<c:if test="${foundStation != null}">
if (stationId != null) {
if (stationDAO.exists(stationId)){
modelAndView.addObject("foundStation", stationDAO.findOne(stationId));
}
}