春天标注不工作 [英] spring annotation not working

查看:172
本文介绍了春天标注不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Spring框架,并希望做annotatios一个HelloWorld,我做了它的工作创造一个控制器和一个视图,基本喂工作我猜;不过,我想既然我不能使用任何SimpleFormController中更多的(de precated)使用annotatios。

我得到的错误是埃斯塔HTTP 404 - /av/index.jsp

我使用NetBeans和我立足的基本模板它提供了范例。我有以下的文件,我是pretty确保它是一个missconfiguration但我无法找到任何能帮助我至今。先谢谢了。

的web.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< web-app的版本=3.0的xmlns =htt​​p://java.sun.com/xml/ns/javaee的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance XSI:的schemaLocation =htt​​p://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\">
    <的context-param>
        <参数-名称>&contextConfigLocation的LT; /参数 - 名称>
        &LT;参数值&GT; /WEB-INF/applicationContext.xml< /参数值&GT;
    &LT; /的context-param&GT;
    &LT;听者GT;
        &LT;监听级&GT; org.springframework.web.context.ContextLoaderListener&LT; /监听级&GT;
    &LT; /听者GT;
    &LT;&servlet的GT;
        &LT; servlet的名称&gt;分派器LT; / servlet的名称&gt;
        &LT;的servlet类&GT; org.springframework.web.servlet.DispatcherServlet&LT; / servlet的类&GT;
        &所述;负载上启动→1&下; /负载上启动&GT;
    &LT; / servlet的&GT;
    &LT; Servlet映射&GT;
        &LT; servlet的名称&gt;分派器LT; / servlet的名称&gt;
        &LT; URL模式&GT; *做&LT; / URL模式&GT;
    &LT; / Servlet映射&GT;
    &LT;会话配置&GT;
        &LT;会话超时&GT;
            三十
        &LT; /会话超时&GT;
    &LT; /会话配置&GT;
    &LT;欢迎-文件列表&gt;
        &LT;欢迎-文件&gt;&redirect.jsp中LT; /欢迎-文件&gt;
    &LT; /欢迎-文件列表&gt;
&LT; / web-app的&GT;

分配器 - servlet.xml中

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;豆的xmlns =htt​​p://www.springframework.org/schema/beans
    XMLNS:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance
    的xmlns:P =htt​​p://www.springframework.org/schema/p
    的xmlns:上下文=htt​​p://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\">    &LT; bean类=org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping/&GT;    &LT;豆的id =annotationHandlerMapping
        类=org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping&GT;
        &LT;属性名=秩序价值=1/&GT;
        &LT;属性名=alwaysUseFullPathVALUE =真/&GT;
    &LT; /豆&GT;    &LT;豆ID =的ViewResolver
    类=org.springframework.web.servlet.view.InternalResourceViewResolver&GT;
        &LT;属性名=preFIX&GT;
            &LT; VALUE&GT; /&LT; /值&GT;
        &LT; /性&gt;
        &LT;属性名=后缀&GT;
            &LT; VALUE&GT;的.jsp&LT; /值&GT;
        &LT; /性&gt;
    &LT; /豆&GT;    &LT;背景:组件扫描基包=控制器/&GT;&LT; /豆&GT;

indexController.java

 封装控制器;进口org.springframework.stereotype.Controller;
进口org.springframework.web.bind.annotation.RequestMapping;
进口org.springframework.web.bind.annotation.RequestMethod;
进口org.springframework.web.portlet.ModelAndView;@Controller
公共类的IndexController {    @RequestMapping(值=/ index.do,方法= RequestMethod.GET)
    公众的ModelAndView INICIO(){
        ModelAndView的MV =新的ModelAndView(指数);
        mv.addObject(usuario,jaxkodex);
        返回MV;
    }
}


解决方案

您缺少

 &LT; MVC:注解驱动/&GT;

在你的配置分配器 - servlet.xml中

请参阅 了解信息

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

这篇关于春天标注不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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