有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring MVC视图似乎不起作用

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

22:18:30.700 [http-nio-8080-exec-6] WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/web_spring/Candidate-module/getCandidate] in DispatcherServlet with name 'webmvc-dispatcher'

为了让他们工作,我把名字弄乱了,但这不是问题所在。我将发布代码,任何帮助都将不胜感激

网络。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>

候补财务总监。爪哇

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";
}
}

投票。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) 个答案

  1. # 1 楼答案

    通过将以下配置添加到webmvc-dispatcher-servlet.xml来启用注释驱动的MVC:

    <mvc:annotation-driven />
    

    这会注册一个RequestMappingHandlerMapping、一个RequestMappingHandlerAdapter、一个ExceptionHandlerExceptionResolver(以及其他),以支持使用注释(例如@RequestMapping@ExceptionHandler等)的注释控制器方法处理请求。 因此,您的配置文件如下所示:

    <?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:mvc="http://www.springframework.org/schema/mvc"
           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/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd 
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context.xsd">
    
        <mvc:annotation-driven />
    
        // same as before
    </beans>