java 弹簧注释不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12664107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:45:10  来源:igfitidea点击:

spring annotation not working

javaspringspring-mvcannotations

提问by jaxkodex

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).

我从 Spring Framework 开始,想用 annotatios 做一个 HelloWorld,我已经让它工作了,创建了一个控制器和一个视图,我猜是基本的 hello 工作;但是,我想使用 annotatios,因为我不能再使用 SimpleFormController(已弃用)。

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

我得到的错误是 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.

我正在使用 Netbeans,我的示例基于它提供的基本模板。我有以下文件,我很确定这是一个错误配置,但到目前为止我找不到任何对我有帮助的东西。提前致谢。

web.xml

网页.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

调度程序-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

索引控制器.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;
    }
}

回答by ccheneson

You are missing

你不见了

<mvc:annotation-driven />

in your config dispatcher-servlet.xml

在你的配置中 dispatcher-servlet.xml

See herefor more info

这里获取更多信息

回答by Florian Fankhauser

You mapped the path /index.doin your controller, so you have to access it with the following url: http://localhost/av/index.do

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

回答by Florian Fankhauser

<mvc:annotation-driven/>will solve out your problem.

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

回答by Ketan

Please try this

请试试这个

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

add an entry in dispatcher-servlet.xml

在 dispatcher-servlet.xml 中添加一个条目

回答by Chathuranga Tennakoon

in your disapatcher-servlet.xml, add <mvc:annotation-driven/>to enable annotation driven handler mapping.

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

回答by Alex Fruzenshtein

Try to go through one of my tutorials:

尝试通过我的教程之一:

Creation of a simple Controllerwith java-based config

使用基于 Java 的配置创建一个简单的控制器

I hope I will get success with it

我希望我能成功