1

编辑:我能够通过重命名index.jsp来解决问题hi.jsp。任何人都知道为什么我的文件名有问题?

问题: 我使用 Java/Spring 编写了 webapp 的后端组件,并通过 Spring 注释将其连接到 Tomcat:

@Controller
public class WorkerSubmitterController {
    @RequestMapping(value = {"/start"}, method = RequestMethod.GET, produces = "text/plain")
    @ResponseBody
    public ResponseEntity<String> start() throws Exception {
        return new ResponseEntity<String>("Started", HttpStatus.OK);
    }
}

我正在像这样配置servlet:

public class WebInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext container) {
        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.scan("com.myProject.someName");

        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(dispatcherContext));

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher =
        container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));

        dispatcher.setLoadOnStartup(1);
        Set<String> mappingConflicts = dispatcher.addMapping("/");
    }

}

当我去的时候这很好用localhost:8080/start

但是,现在我正在尝试通过index.jsp工作来编写前端。我在下面创建了一个index.jsp文件/webapp/WEB-INF/

<html>
<head>
    <title>Hello world</title>
</head>
<body>

我添加了一个web.xml文件:

<web-app id="WebApp_ID" version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <servlet>
        <servlet-name>home</servlet-name>
        <jsp-file>/index.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>home</servlet-name>
        <url-pattern>/home</url-pattern>
    </servlet-mapping>

</web-app>

当我访问时,localhost:8080/home 我收到此错误:

HTTP ERROR 404

Problem accessing /home. Reason:

    Not Found
Powered by Jetty://

服务器端显示:

2013-10-08 20:27:31.652:WARN:oejs.ServletHandler:/home
java.lang.NullPointerException
    at java.net.URLEncoder.encode(URLEncoder.java:205)
    at java.net.URLEncoder.encode(URLEncoder.java:171)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:474)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:378)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:669)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:457)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1075)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:384)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1009)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
    at org.eclipse.jetty.server.Server.handle(Server.java:368)
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489)
    at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:942)
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1004)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:640)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
    at java.lang.Thread.run(Thread.java:724)

我究竟做错了什么?我是否错过了让 .jsp 页面正常工作的步骤?不index.jsp正确吗?我已经尝试修改index.jsp为只是hello并且弹出相同的错误。另外,我尝试移动index.jsp文件以找到问题的根源,错误消失了,但后来我得到了

Oct 08, 2013 9:58:38 PM org.apache.jasper.servlet.JspServlet serviceJspFile
SEVERE: PWC6117: File "%2FUsers%2FuserName%2Fpersonal%2Friver%2Fsrc%2Fmain%2Fwebapp%2FWEB-INF%2Findex.jsp" not found 

所以我猜这个问题与我的 JSP 文件有关。

4

1 回答 1

2

问题是您正试图访问WEB-INF目录中的 JSP,这显然是不可访问的。你正试图从根目录访问它,这就是你得到错误的原因HTTP ERROR 404

However, now I'm trying to write the frontend by getting index.jsp working. I created a index.jsp file under /webapp/WEB-INF/ with the following:...

尝试将文件夹外的 JSP 移动WEB-INF到应用程序的根目录中。

于 2013-10-09T06:05:59.593 回答