我是新來的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;
}
}
應用結構
登錄:
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'
你的鏈接應該是'/ index'而不是jsp。另外如果yu可以使用你在'index.jsp'中的鏈接,那麼你的jsp就不是你告訴它彈簧的地方。 –
Deinum,對不起,我沒有得到。請你能告訴我應該改變什麼/在哪裏? – Muhammad
我說的。將文件中的鏈接更改爲'/ index' ... –