我正在使用Spring WEB和SMartgwt,但我需要将此页面作为JSP提供。在Jsp中,我需要使用已在web.xml中配置的servlet从数据库返回一个字符串。 jsp的代码是这样的:
<%@ page import="com.sgss.nove.server.property.PropertyManagerWebImpl" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%!String url; %>
<%
PropertyManagerWebImpl manager = new PropertyManagerWebImpl();
manager.init();
url = manager.getPropertyValueForJsp("Category", "Name");
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- <meta http-equiv="x-ua-compatible" content="IE=7" > -->
<title>MyTitle</title>
<script type="text/javascript" src="<%=url%>"></script>
<script>
//the rest of the code
servlet中的方法如下所示:
public String getPropertyValueForJsp(String category, String name) {
Property prop = BObjectFactory.getPropertyManager(this).getPropertyById(category, name);
PropertyBean bean = PropertyMapping.mapFromEntityToBean(prop);
return bean.getValore();
}
BObjectFactory
中的方法如下:
public static IPropertyManager getPropertyManager(final GenericServlet servlet) {
ServletContext sContext = servlet.getServletContext();
ApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(sContext);
return appContext.getBean("propertyManager", IPropertyManager.class);
}
它突破了这一行:ServletContext sContext = servlet.getServletContext();
servlet在web.xml
:
<servlet>
<servlet-name>PropertyManagerService</servlet-name>
<servlet-class>com.sgss.nove.server.property.PropertyManagerWebImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PropertyManagerService</servlet-name>
<url-pattern>/mysite/PropertyManagerService</url-pattern>
</servlet-mapping>
我忘了错误是Null指针,堆栈跟踪如下:
java.lang.NullPointerException
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:123)
com.sgss.nove.common.BObjectFactory.getPropertyManager(BObjectFactory.java:157)
com.sgss.nove.server.property.PropertyManagerWebImpl.getPropertyValueForJsp(PropertyManagerWebImpl.java:83)
org.apache.jsp.tableau_jsp._jspService(tableau_jsp.java:130)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.sgss.nove.UserSessionControlFilter.doFilter(UserSessionControlFilter.java:130)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
com.sgss.nove.GWTCacheControlFilter.doFilter(GWTCacheControlFilter.java:56)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)