Spring MVC控制器没有返回值

时间:2016-06-10 12:11:55

标签: spring-mvc

我是初学mvc的新手。我试图从我的helloworld.jsp上的模型视图控制器获得响应。当我运行应用程序时,我只在页面上定义了$ {message} $ {name}。我在控制器文件中使用了sysout,但在控制台上也没有得到任何东西。请问你能查看我遗失的内容吗?

spring-mvc version:4.2.6.RELEASE

的index.jsp

        <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <!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>Spring 4 MVC - HelloWorld Index Page</title>
        </head>
        <body>

            <center>
                <h2>Hello World</h2>
                <h3>
                    <a href="helloworld.jsp">Click Here</a>
                </h3>
            </center>
        </body>
        </html>

的helloWorld.jsp

<html>
<head>
<title>Spring 4 MVC -HelloWorld</title>
</head>
<body>
        <h2>${message} ${name}</h2>


</body>
</html>

的web.xml

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

  <display-name>FirstSpringMVCProject</display-name>

   <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>
                  org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
   </servlet>

  <servlet-mapping>
    <servlet-name>spring-dispatcher</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:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


  <context:component-scan base-package="com.test.testcontroller" />
  <mvc:annotation-driven/>

  <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
 </bean>

 </beans>

TestController.java

package com.test.testcontroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TestController {

    @RequestMapping("/index")
    public ModelAndView showMessage() {
        System.out.println("in controller");

        ModelAndView mv = new ModelAndView("helloworld");
        mv.addObject("message", "this is a message for");
        mv.addObject("name", "Spring");
        return mv;
    }

}

申请结构

enter image description here

日志:

INFO: Initializing Spring FrameworkServlet 'spring-dispatcher'
Jun 13, 2016 9:30:23 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'spring-dispatcher': initialization started
Jun 13, 2016 9:30:23 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'spring-dispatcher-servlet': startup date [Mon Jun 13 09:30:23 BST 2016]; root of context hierarchy
Jun 13, 2016 9:30:23 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]
Jun 13, 2016 9:30:24 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'spring-dispatcher-servlet': startup date [Mon Jun 13 09:30:23 BST 2016]; root of context hierarchy
Jun 13, 2016 9:30:24 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'spring-dispatcher-servlet': startup date [Mon Jun 13 09:30:23 BST 2016]; root of context hierarchy
Jun 13, 2016 9:30:24 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'spring-dispatcher': initialization completed in 1211 ms
Jun 13, 2016 9:30:24 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TestApp/index] in DispatcherServlet with name 'spring-dispatcher'

5 个答案:

答案 0 :(得分:0)

这应该是更新的index.jsp。此外,我们将链接作为索引,因为您的请求映射仅为/ index。

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <!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>Spring 4 MVC - HelloWorld Index Page</title>
        </head>
        <body>

            <center>
                <h2>Hello World</h2>
                <h3>
                    <a href="index">Click Here</a>
                </h3>
            </center>
        </body>
        </html>

在tomcat控制台日志中检查此行

INFO: Mapped "{[/index]}" onto public org.springframework.web.servlet.ModelAndView com.test.testcontroller.TestController.showMessage()

如果不存在则意味着请求映射未完成。请参阅下面更新的web.xml

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

    <display-name>FirstSpringMVCProject</display-name>

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

完成所有这些后,您将获得

HTTP Status 404 - /spring-mvc-check/WEB-INF/helloworld.jsp

要解决这个问题,请将helloworld.jsp放入WEB-INF。

使用pom.xml

更新
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.stackoverflow</groupId>
    <artifactId>spring-mvc-check</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jsp-api-2.0</artifactId>
            <version>6.1.26</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
        </plugins>
    </build>
</project>

答案 1 :(得分:0)

尝试将以下内容添加到spring-dispatcher servlet

下的web.xml中
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>

答案 2 :(得分:0)

使用maven创建spring mvc项目似乎存在一些问题。由于某些原因,它不会生成.class文件。我尝试了各种选择,但没有一个适合我。

然后我创建了一个简单的动态Web项目,它的工作正常。

我仍然想知道maven为什么不起作用。

感谢您的帮助。

答案 3 :(得分:0)

您可以使用设置默认的Spring(sts)而不是设置中的问题

首先,您不使用index.jsp。 只需使用helloworld.jsp。

(控制器)

package com.test.testcontroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TestController {

    @RequestMapping(value = "/show/message")
    public ModelAndView showMessage(ModelAndView mv) {
        System.out.println("in controller");

        mv.addObject("message", "this is a message for");
        mv.addObject("name", "Spring");
        mv.setViewName("/helloworld");

        return mv;
    }

}

在浏览器中输入一个url(你是localhost / context path / show / message)。 你成功地展示了信息。

如果你想使用index.jsp (你index.jsp)

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!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>Spring 4 MVC - HelloWorld Index Page</title>
    </head>
    <body>

        <center>
            <h2>Hello World</h2>
            <h3>
                <a href="/show/message">Click Here</a>
            </h3>
        </center>
    </body>
    </html>

您在控制器中添加代码

@Controller
public class TestController {

....

    @RequestMapping(value = "/index")
    public String showIndex() {

        return "/index";
    }

}

答案 4 :(得分:-1)

默认EL已禁用。

添加

<%@ page isELIgnored="false" %>

启用jsp。