【问题标题】:Spring Class Controller Doesn't workSpring 类控制器不起作用
【发布时间】:2017-06-24 12:38:40
【问题描述】:

我尝试做一个简单的 Spring 项目。我使用服务器 Tomcat 7.0。当我将http://localhost:8090/springProject/ 写入我的网址时,它可以正常工作。但是当我尝试使用类控制器(使用@RequestMapping("/hello"))时它不起作用。我尝试使用很多解决方案,但没有任何效果。有什么建议么?谢谢

类控制器:

package it.spring;

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

@Controller
public class HelloWorldController {
String message = "Welcome to Spring MVC!";

@RequestMapping("/hello")
public ModelAndView showMessage(
        @RequestParam(value = "name", required = false, defaultValue = "World") String name) {

    ModelAndView mv = new ModelAndView("helloworld");
    mv.addObject("message", message);
    mv.addObject("name", name);
    return mv;
}
}

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" id="WebApp_ID" 
version="2.5">

<display-name>Archetype Created Web Application</display-name>

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

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

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

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

调度程序-servlet:

<?xml version="1.0" encoding="utf-8"?>
<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="it.spring"/>

    <bean

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

查看 helloworld.jsp(在 src/main/webapp/WEB-INF/views 内):

 <%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' prefix="c"%>
 <%@ 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</title>
</head>
<body>
<center>
    <h2>Hello World</h2>
    <h2>${message} ${name}</h2>
</center>
</body>
</html>

【问题讨论】:

  • 出现:HTTP状态404 - /springProject/hello
  • 请贴出代码
  • 如果您希望有人给您一个建设性的答案,您需要将所有相关代码和配置详细信息添加到原始问题中
  • 您的控制器类似乎在默认包中。但是您正在扫描it.spring。 Yu 也没有发表您的观点,也没有告诉我们它是如何命名的以及它的位置。用旧的 tomcat 7、非常旧的 2.5 webapp 版本、旧的 XML spring 配置而不是基于 Java 的配置、不使用 spring-boot 来启动一个新项目也很奇怪。

标签: java spring maven


【解决方案1】:

您需要在 dispatcher-servlet.xml 中启用注解驱动的 Spring MVC 控制器:

<mvc:annotation-driven>

【讨论】:

    【解决方案2】:

    您正在使用基于 XML 的配置,因此您可以使用:

    <mvc:annotation-driven/> 
    

    启用 MVC。

    如果您要使用基于注释的配置,请使用

     @EnableWebMvc
    

    启用 MVC。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-19
      • 2017-04-07
      • 2015-11-15
      • 2012-06-16
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多