简体   繁体   中英

spring annotation not working

I am starting with Spring Framework and want to do a HelloWorld with annotatios, I've made it work creating a controller and a view, basic hello work I guess; however, I want to use annotatios since I can not use SimpleFormController any more (deprecated).

The error I am getting is Estado HTTP 404 - /av/index.jsp

I am using Netbeans and I am basing the example on the basic template It provides. I have the following files, I am pretty sure it is a missconfiguration but I can't find anything that would help me so far. Thanks in advance.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <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>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

dispatcher-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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <bean id="annotationHandlerMapping" 
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="order" value="1"/>
        <property name="alwaysUseFullPath" value="true"/>
    </bean>

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

    <context:component-scan base-package="controller"/>

</beans>

indexController.java

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;

@Controller
public class IndexController {

    @RequestMapping(value="/index.do", method= RequestMethod.GET)
    public ModelAndView inicio (){
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("usuario", "jaxkodex");
        return mv;
    }
}

You are missing

<mvc:annotation-driven />

in your config dispatcher-servlet.xml

See here for more info

您在控制器中映射了路径/index.do ,因此您必须使用以下 URL 访问它: http://localhost/av/index.do

<mvc:annotation-driven/>将解决您的问题。

Please try this

<bean 
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 

add an entry in dispatcher-servlet.xml

在您的 disapatcher-servlet.xml 中,添加<mvc:annotation-driven/>以启用注释驱动处理程序映射。

Try to go through one of my tutorials:

Creation of a simple Controller with java-based config

I hope I will get success with it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM