Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧控制器_Java_Spring Mvc_Spring Annotations - Fatal编程技术网

Java 弹簧控制器

Java 弹簧控制器,java,spring-mvc,spring-annotations,Java,Spring Mvc,Spring Annotations,我试图遵循Spring教程,但一直收到404错误。下面是我的控制器类: @Controller @RequestMapping("/updatedescription.htm") public class UpdateDescriptionController { /** Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass());

我试图遵循Spring教程,但一直收到404错误。下面是我的控制器类:

@Controller
@RequestMapping("/updatedescription.htm")
public class UpdateDescriptionController {
    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    @Autowired
    private ProductManager productManager;

    @RequestMapping(value="/{description}", method=RequestMethod.GET) 
    public String updateDescription(@PathVariable("description") String description, Model model) {       
        return "redirect:/hello.htm";
    }

    @Autowired
    public UpdateDescriptionController(ProductManager productManager) {
        this.productManager = productManager;
    }
}
当我试着去“http://localhost:8505/baseSpringMVC/updatedescription.htm/Chair“我得到一个404错误

我已将web.xml文件设置为将所有*.htm流量指向DispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:javaee="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5">   
  <servlet>
    <servlet-name>baseSpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>baseSpringMVC</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

baseSpringMVC
org.springframework.web.servlet.DispatcherServlet
1.
baseSpringMVC
*.htm
index.jsp
baseSpringMVC-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"
        xmlns:oxm="http://www.springframework.org/schema/oxm"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">

    <!-- the application context definition for the springapp DispatcherServlet -->

    <context:component-scan base-package="springapp.controllers"/>

    <!-- Load dummy data -->
    <bean id="productManager" class="springapp.model.SimpleProductManager">
        <property name="products">
            <list>
                <ref bean="product1"/>
                <ref bean="product2"/>
                <ref bean="product3"/>
            </list>
        </property>
    </bean>

    <bean id="product1" class="springapp.model.Product">
        <property name="description" value="Lamp"/>
        <property name="price" value="5.75"/>
    </bean>

    <bean id="product2" class="springapp.model.Product">
        <property name="description" value="Table"/>
        <property name="price" value="75.25"/>
    </bean>

    <bean id="product3" class="springapp.model.Product">
        <property name="description" value="Chair"/>
        <property name="price" value="22.79"/>
    </bean>

    <!--  pull in messages from <classpath>/messages.properties -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"/>
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

任何帮助都会很好


谢谢大家!

将/Chair添加到末尾时,它不是*.htm。发布您的web.xml,以便我们可以调试它。

将方法更改为Post,测试它,并让我知道,以便我们可以进一步调试

@RequestMapping(value="/{description}", method=RequestMethod.POST)

删除控制器上的顶级RequestMapping注释。updateDescription方法将指定请求映射。

类级请求映射不应包含“.htm”。即改变:

@RequestMapping("/updatedescription.htm") 

如果这不能解决问题,我们可能需要查看您的Spring配置文件

编辑:

Spring配置文件的顶部,包括MVC名称空间,应该如下所示:

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


这也不起作用。它来自一个链接,所以我相信它一定是一个GET。你有下面的一行吗:是的,在baseSpringMVC-servlet.xml文件中。否,我在工作,他们在这里屏蔽这些站点。它来自Spring教程,您在配置部分15.3.2中有什么想法。表示您可以使用顶级控制器,每个方法都有另一个mappingupdated链接是的,您可以,但它至少不应该包含“.htm”扩展名。您应该使URL等效于“”。请按照下面的建议进行尝试。我将添加我的*-servlet.xml文件尝试将其添加到Spring配置文件:
Stacktrace:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:ServletContext资源[/WEB-INF/BaseS普林gMVCServlet.xml]的xml文档中的第14行无效;嵌套异常为org.xml.sax.saxpasseeption:元素“mvc:annotation-driven”的前缀“mvc”未绑定。在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)的org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)中,您需要在文件顶部定义mvc命名空间。所以您需要
xmlns:mvc=”http://www.springframework.org/schema/mvc“
xsi:schemalocation
中,您需要
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
。请您澄清一下,以便我可以剪切和粘贴。
<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" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:oxm="http://www.springframework.org/schema/oxm" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">