2016-06-10 113 views
0

我是新來的spring mvc。我試圖從我的helloworld.jsp上的模型視圖控制器獲取響應。當我運行應用程序時,我只能在頁面上定義$ {message} $ {name}。我在控制器文件中使用了sysout,但沒有在控制檯上獲取任何東西。請你能檢查我缺少的東西嗎?Spring MVC控制器不返回值

彈簧MVC版本: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' 
+0

你的鏈接應該是'/ index'而不是jsp。另外如果yu可以使用你在'index.jsp'中的鏈接,那麼你的jsp就不是你告訴它彈簧的地方。 –

+0

Deinum,對不起,我沒有得到。請你能告訴我應該改變什麼/在哪裏? – Muhammad

+0

我說的。將文件中的鏈接更改爲'/ index' ... –

回答

0

以下到web.xml中似乎有一些問題與使用maven創建Spring MVC的項目。由於某些原因,它不會生成.class文件。我嘗試了各種選擇,但都沒有爲我工作。

然後我創建了一個簡單的動態web項目,它的工作正常。

我還想知道爲什麼maven沒有工作。

感謝您的幫助。

0

這應該是更新的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="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> 
+0

得到這個錯誤---------------- 2016年6月10日下午4:08:16 org.springframework.web.servlet.PageNotFound noHandlerFound 警告:找不到HTTP請求的映射DispatcherServlet中名稱爲「spring-dispatcher」的URI [/ TestApp/index] – Muhammad

+0

您可以粘貼您的應用程序啓動日誌嗎?您已經嘗試過/ index,現在您還嘗試了/ TestApp/index。你有沒有在啓動時看到/索引映射? –

+0

更新後的啓動日誌.. – Muhammad

0

嘗試添加下彈簧調度的servlet

<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> 
+0

我已經嘗試添加這個標籤,但獲取錯誤---- cvc-complex-type.2.4.a:發現無效內容從元素'init-param' – Muhammad

+0

發現你可以發佈你的整個web.xml和servlet.xml ? –

+0

已更新後的更改。 – Muhammad

0

您在設置使用設置默認春(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(你本地主機/上下文路徑/顯示/消息)。 你成功顯示消息。

,如果你想使用的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"; 
    } 

} 
-1

默認添加代碼EL被禁用。

添加

<%@ page isELIgnored="false" %> 

把jsp啓用它。