Spring MVC视图似乎不起作用

时间:2016-04-07 21:32:04

标签: java spring jsp spring-mvc

我在一个简单的Spring MVC初学者教程中映射视图时遇到了一些麻烦,我一直遇到同样的错误:

  

22:18:30.700 [http-nio-8080-exec-6] WARN osweb.servlet.PageNotFound - 在名为'的DispatcherServlet中找不到带有URI [/ web_spring / Candidate-module / getCandidate]的HTTP请求的映射webmvc-调度'

我已经弄乱了试图让他们上班的名字,但这不是问题。我将发布代码,任何帮助将不胜感激!

的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"     
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>web_spring</display-name>


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


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


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/webmvc-dispatcher-servlet.xml</param-value>
</context-param>

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


</web-app>

webmvc-调度-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:context="http://www.springframework.org/schema/context"
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-3.1.xsd">


 <context:component-scan base-package="ie.cit.oossp" />

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



</beans>

CandidateController.java

package web_spring.controller;

import java.util.List;
import java.util.Map;

import web_spring.beans.Candidate;
import web_spring.service.CandidateService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/Candidate-module")
public class CandidateController {


@Autowired
private CandidateService service;  

@RequestMapping(value = "/getCandidate", method = RequestMethod.GET)
public String index(ModelMap model){
    model.addAttribute("msg", "Hello Spring Web App");
    return "vote";
}
}

vote.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Vote here</title>
</head>
<body>

<h2>Vote : ${msg}</h2>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

通过将以下配置添加到webmvc-dispatcher-servlet.xml

来启用注释驱动的MVC
<mvc:annotation-driven />

注册RequestMappingHandlerMappingRequestMappingHandlerAdapterExceptionHandlerExceptionResolver(以及其他)以支持使用带注释的控制器方法处理请求,使用注释@RequestMapping,{ {1}}和其他人。 所以你的配置文件是这样的:

@ExceptionHandler