在Spring 3.0中获取servlet异常

时间:2014-07-19 08:19:22

标签: java xml spring spring-mvc servlets

试图从这个链接学习弹簧:

http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/

但是收到以下错误:

    type Exception report

    message Servlet execution threw an exception

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

    exception

   javax.servlet.ServletException: Servlet execution threw an exception
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

在浏览器和eclipse控制台上重复多次重复错误。

The dir structure is

文件内容如下: 的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>Spring3MVC</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

 <servlet>
 <servlet-name>spring</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>spring</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>

</web-app>

弹簧servlet.xml中

   <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


        <context:component-scan
            base-package="siso.springrolls.controller" />

        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/" />
            <property name="suffix" value=".html" />
        </bean>
    </beans>

控制器是:

@Controller
public class HomeController {

    @RequestMapping("/StartPage")
    public ModelAndView springContoller(){
        String message = "Hey welcome to Spring 3.0!! ";
        System.out.println("in controller");
        return new ModelAndView("StartPage","message",message);
    }
}

的index.jsp

</head>
<body>
<a href="StartPage.html">Click here </a>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

这个问题是由于混合了弹簧罐。我遇到了同样的问题。从pom.xml中删除了一些罐子并且它有效。 这是我原来的pom.xml。使用Spring 4 Hibernate 4的基础项目

    <properties>
        <java-version>1.7</java-version>
        <org.springframework-version>4.0.3.RELEASE</org.springframework-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.5.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>



        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>11.2.0.2.0</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>

</dependencies>

删除了以下依赖项,它运行良好

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>

更新的答案: -

添加网址映射:

 <url-pattern>/*</url-pattern>

这应该可以胜任。

答案 1 :(得分:0)

浪费了5个小时:(

任何人都可以告诉我为什么以下代码不起作用:

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

但如果我将其更改为:

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

它工作正常。修改后的目录结构为:

enter image description here

为什么它没有拿起html文件并抛出 在DispatcherServlet 中找不到带有URI的HTTP请求和使用JSP的工作文件的映射。

答案 2 :(得分:0)

尝试更新该部分代码并尝试

 <servlet-mapping>
 <servlet-name>spring</servlet-name>

 //Use this 
 <url-pattern>/*</url-pattern>

That is not correct
<url-pattern>/</url-pattern>

 </servlet-mapping>

更新

尝试添加此

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>