与Spring的控制器连接不起作用

时间:2018-03-22 03:26:20

标签: java spring

我尝试按照教程学习Spring。我使用Netbeans,看起来Spring没有“看到”控制器,但我在Apache日志中看不到任何有意义的错误。

每当我按下表单的提交按钮时,我都会收到404错误。我甚至在网上搜索过。我想了解错误,而不仅仅是复制和粘贴一些东西。

的index.jsp

<html>
<body>
<h2>Hello World!</h2>
<form action="add">
    <input type="text" name="t1"><br />
    <input type="text" name="t2"><br />
    <input type="submit">
</form>
</body>
</html>

ffuentese-servlet.xml中

<?xml version="1.0" encoding="windows-1252"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:ctx="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-2.5.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd ">



    <ctx:annotation-config></ctx:annotation-config>
    <ctx:component-scan base-package="com.ffuentese"></ctx:component-scan>

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

AddController.java

package com.ffuentese;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Francisco
 */
@Controller
public class AddController 
{

    @RequestMapping("/add")
    public String add(){
        return "display.jsp";
    }
}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app 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"
     version="3.0">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <servlet>
        <servlet-name>ffuentese</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ffuentese</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

</web-app>

1 个答案:

答案 0 :(得分:0)

您在web.xml中的配置不正确。

将网址格式从“ .htm”更改为“/ ”。这样,来自应用程序服务器的每个请求都将转发到Spring调度程序servlet。