未调用Spring MVC Controller [英] Spring MVC Controller not called

查看:84
本文介绍了未调用Spring MVC Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Web开发的初学者,并尝试创建一个简单的spring mvc应用程序.

I'm beginner in web development and try to create a simple spring mvc application.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>Spring Web MVC Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>
    <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>*.do</url-pattern>
    </servlet-mapping>
</web-app>

mvc-dispatcher-servlet.xml

<?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">

    <mvc:annotation-driven/>

    <context:component-scan base-package="org.test.test.controller"/>
    <context:component-scan base-package="org.test.test.model"/>

    <bean id="ViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

控制器

@Controller
@Scope(value = "singleton")
public class T1Controller {

    @RequestMapping(value = "/hello.do")
    public ModelAndView helloWorld() {
        System.out.print("Hi");
        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message);
    }
}

我编写的所有内容都与我的教程一样正确,但仍然出现 404 tomcat 8.5RC 错误.我没有调用控制器方法.

I have written everything correct like my tutorial but still get 404 tomcat 8.5RC error. I thing controller method is not called.

我添加了 spring 4 libs ,我觉得一切都很好,但是在Intellij IDEA中看不到 Hi 输出.

I have add spring 4 libs and I thing everything was fine but I don't see Hi output in Intellij IDEA.

hello.jsp

hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>one</title>
</head>
<body>
Hello World
${message}
</body>
</html>

运行tomcat时,我看到我的 index.jsp 页...但是当尝试进入 http://localhost:8000/hello.do 时,得到> 404 错误:(

I see my index.jsp page when tomcat is run ... but when try to go http://localhost:8000/hello.do getting 404 error :(

推荐答案

首先,我将首先扫描一个基本软件包,而不是两个.像这样从路径中删除 controller 映射:

First I would start by scanning only one base package instead of two. Remove the controller map from the path aswel Like this:

<context:component-scan base-package="org.test.test"/>

除去以下内容,因为只有一个就足够了.您不需要两个.

Get rid of the following because only one will suffice. You dont need two.

<context:component-scan base-package="org.test.test.model"/> 

并按如下所示修改您的属性值:

And modify your property value as follows:

    <property name="prefix">
        <value>/WEB-INF/</value>
    </property>

这篇关于未调用Spring MVC Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆