Java 404错误弹簧MVC
我已经编写了一个SpringMVC应用程序,它在XML视图中显示测试用例和套件的报告 我的controller.java类如下所示Java 404错误弹簧MVC,java,web-applications,servlets,spring-mvc,Java,Web Applications,Servlets,Spring Mvc,我已经编写了一个SpringMVC应用程序,它在XML视图中显示测试用例和套件的报告 我的controller.java类如下所示 package com.DrAssist.Fitnesse.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web
package com.DrAssist.Fitnesse.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.DrAssist.Fitnesse.model.Suite;
@Controller
@RequestMapping("/Suite/{name}")
public class XMLController {
@RequestMapping(value="{name}", method = RequestMethod.GET, headers = "content-type=application/xml")
public @ResponseBody Suite getSuiteInXML(@PathVariable String name) {
Suite suite = new Suite("1",name,"3","Test1","4","5","7","9","550");
return suite;
}
}
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.DrAssist.Fitnesse.controller" />
<mvc:annotation-driven />
<!--
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="xmlViewer"
class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.DrAssist.Fitnesse.model</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
-->
</beans>
我的web.xml文件如下所示
package com.DrAssist.Fitnesse.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.DrAssist.Fitnesse.model.Suite;
@Controller
@RequestMapping("/Suite/{name}")
public class XMLController {
@RequestMapping(value="{name}", method = RequestMethod.GET, headers = "content-type=application/xml")
public @ResponseBody Suite getSuiteInXML(@PathVariable String name) {
Suite suite = new Suite("1",name,"3","Test1","4","5","7","9","550");
return suite;
}
}
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.DrAssist.Fitnesse.controller" />
<mvc:annotation-driven />
<!--
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="xmlViewer"
class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.DrAssist.Fitnesse.model</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
-->
</beans>
SpringWebMVC应用程序
mvc调度器
org.springframework.web.servlet.DispatcherServlet
1.
mvc调度器
/*
上下文配置位置
/WEB-INF/mvc-dispatcher-servlet.xml
org.springframework.web.context.ContextLoaderListener
和dispatcher-servlet.xml文件如下所示
package com.DrAssist.Fitnesse.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.DrAssist.Fitnesse.model.Suite;
@Controller
@RequestMapping("/Suite/{name}")
public class XMLController {
@RequestMapping(value="{name}", method = RequestMethod.GET, headers = "content-type=application/xml")
public @ResponseBody Suite getSuiteInXML(@PathVariable String name) {
Suite suite = new Suite("1",name,"3","Test1","4","5","7","9","550");
return suite;
}
}
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.DrAssist.Fitnesse.controller" />
<mvc:annotation-driven />
<!--
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="xmlViewer"
class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.DrAssist.Fitnesse.model</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
-->
</beans>
我得到一个404错误,控制台显示错误为
在名为“mvc dispatcher”的DispatcherServlet中找不到URI为[/SpringMVC/Suite/suite1]的HTTP请求的映射。
请注意,SpringMVC部分/SpringMVC/Suite/suite1]本身似乎没有通过请求
提前感谢您将
{name}
路径变量放在两个@RequestMapping
中。试着离开第一个,就像@RequestMapping(“Suite”)
一样,您的代码将无法工作,因为log语句是在DispatcherServlet中编写的,并且在那里进行了很好的硬编码。除了复制DispatcherServlet
替换生成错误响应代码的代码,并将其重定向到所需的输出之外,您可以做的很少。在web.xml中,您将dispatcher声明为mvc dispatcher,并且您提供的spring配置文件名为dispatcher-servlet.xmlmvc-dispatcher-servlet.xml。试试这个。如果你已经写过了
@RequestMapping(value="{name}"
比你不需要写这个,这是错误的。请更改
@RequestMapping("/Suite/{name}")
改写
@RequestMapping("/Suite")
还有一件事我想补充的是,我从Mykong导入了一个程序,使其工作,然后根据我的要求对相同的代码进行了更改。这是否会影响构建,这是它不能正常运行的原因之一。谢谢我尝试了许多排列和组合,但都不起作用。我的项目名称是SpringMVC因此,我的URL看起来甚至尝试过使用SpringMVC,但它不起作用