0

我在显示来自 Tomcat 服务器的 register.jsp 网页时遇到问题。register.jsp 位于 WEB-INF/jsp 和 webapps/register 文件夹,RegisterController.class 位于 WEB-INF/classes。我在两个地方都有 register.jsp,因为我试图让 register.jsp 工作,但最终 register.jsp 将驻留在 WEB-INF/jsp 中。问题似乎出在 web.xml 或 register-servlet.xml 中。我正在使用 Tomcat 7.0.21,Spring MVC 3.0。

web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 

         xsi:schemaLocation="
         http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         id="WebApp_ID" version="2.5">      

  <display-name>Snap Fires</display-name>
  <servlet>
        <servlet-name>register</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet-mapping>
        <servlet-name>register</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>



</web-app>

register-servlet.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"

    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix">
           <value> /WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
          <value>.jsp</value>
        </property>
    </bean>

    <bean name="/register.jsp" class="snapFires.controller.RegisterController"/>

</beans>

错误代码:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.register_jsp
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:178)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.ClassNotFoundException: org.apache.jsp.register_jsp
    java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    java.security.AccessController.doPrivileged(Native Method)
    java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:132)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:172)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.21 logs.
Apache Tomcat/7.0.21

register.jsp 文件

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Registration Page</title>
</head>
<body>

<h1>Create New Person</h1>


<form:form action="/jsp/registerOut.html" commandName="register">
 <table>
  <tr>
   <td><form:label path="firstName">First Name:</form:label></td>
   <td><form:input path="firstName"/></td>
  </tr>

  <tr>
   <td><form:label path="lastName">Last Name</form:label></td>
   <td><form:input path="lastName"/></td>
  </tr>
 </table>

 <input type="submit" value="Save" />
</form:form>

</body>
</html>

RegisterController.java 类

package snapFires.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
//import javax.annotation.Resource;

import org.apache.log4j.Logger;
//import org.springframework.ui.ModelMap;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ModelAttribute;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
//import org.springframework.web.servlet.mvc.SimpleFormController;
import java.io.IOException;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import snapFires.domain.Name;
import snapFires.service.RegisterImpl;

@Controller
@RequestMapping("/register")
public class RegisterController extends MultiActionController {


    public ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return new ModelAndView("register.jsp");
    }
    //@Resource(name="register")
    //private RegisterImpl registerImpl;
    protected static Logger logger = Logger.getLogger("controller");

    @RequestMapping(value="/jsp/register.html", method=RequestMethod.GET)
    public void simpleForm(Model model){
        model.addAttribute(new RegisterImpl());
    }

    @RequestMapping(value="/jsp/registerOut.html", method=RequestMethod.POST)
    public void simple(@ModelAttribute RegisterImpl registerImpl, Model model){
        model.addAttribute("register", registerImpl);
    }



}
4

1 回答 1

0

在 RegisterController 类中返回 ModelAndView 时只返回寄存器。由于您使用的是带后缀和前缀的内部视图解析器,因此它将添加 WEB-INF/jsp 和 .jsp 来注册并获取路径。

另请参阅https://github.com/SpringSource/spring-mvc-showcase以获取带有 pdf 的 spring mvc3 展示。请阅读http://duckranger.com/2011/07/spring-mvc-3-0-with-sts-tutorial-part-i/

于 2012-08-02T13:41:17.133 回答