我正在尝试使用JSTL标记将User类中的属性username
设置为newUser
,但是当我输入以下链接http://localhost:8082/LoginApp/seccuss.jsp
时,我会收到以下错误。 setProperty
如何标记JSTL中的单词?
seccusss.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="org.user.dto.User" %>
<!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>Seccuss</title>
</head>
<body>
<h3>login Successful</h3>
<jsp:useBean id="user" class="org.user.dto.User" scope="request">
<jsp:setProperty property="username" name="newUser"/>
</jsp:useBean>
Hello <jsp:getProperty property="username" name="user"/>
</body>
</html>
用户类:
package org.user.dto;
public class User {
private String username;
private String userId;
public String getUsername() {
return username;
}
public void setUsername(String userName) {
this.username = userName;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
错误消息:
Mai 29, 2016 1:18:03 PM org.apache.catalina.core.StandardWrapperValve invoke
SCHWERWIEGEND: Servlet.service() for servlet [jsp] in context with path [/LoginApp] threw exception [An exception occurred processing JSP page /seccuss.jsp at line 22
19:
20: <%--The previous code line with JSTL --%>
21: <jsp:useBean id="user" class="org.user.dto.User" scope="request">
22: <jsp:setProperty property="username" name="newUser"/>
23: </jsp:useBean>
24: Hello <jsp:getProperty property="username" name="user"/>
25:
Stacktrace:] with root cause
java.lang.NullPointerException
at org.apache.jasper.runtime.JspRuntimeLibrary.internalIntrospecthelper(JspRuntimeLibrary.java:320)
at org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:306)
at org.apache.jsp.seccuss_jsp._jspService(seccuss_jsp.java:108)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:2)
您要在标记中提供name属性的值,“ newUser ”必须是bean的名称,例如用户,就像您在标记中所做的那样。还提供值属性,否则将打印 null
试试这个。
<jsp:useBean id="user" class="com.model.User" scope="request">
<jsp:setProperty property="username" name="user" value="hiren"/>
</jsp:useBean>
Hello <jsp:getProperty property="username" name="user"/>