gpt4 book ai didi

java - spring注释不起作用

转载 作者:行者123 更新时间:2023-12-01 18:59:43 24 4
gpt4 key购买 nike

我从 Spring 框架开始,想要做一个带有注释的 HelloWorld,我已经让它创建了一个 Controller 和一个 View ,我猜是基本的 hello 工作;但是,我想使用注释,因为我不能再使用 SimpleFormController (已弃用)。

我收到的错误是 Estado HTTP 404 -/av/index.jsp

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

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>

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

最佳答案

你失踪了

<mvc:annotation-driven />

在您的配置中dispatcher-servlet.xml

参见here了解更多信息

关于java - spring注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12664107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com