Java Spring请求映射不工作
我试图用链接调用控制器,但它不起作用。所有注释配置和url模式都在配置文件中指定。这是我的密码 dispatcher-servlet.xml:Java Spring请求映射不工作,java,xml,spring,spring-mvc,annotations,Java,Xml,Spring,Spring Mvc,Annotations,我试图用链接调用控制器,但它不起作用。所有注释配置和url模式都在配置文件中指定。这是我的密码 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
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!-- Scan only for @Controllers -->
<context:component-scan base-package="com.controller" use-default-filters="false" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<mvc:annotation-driven />
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
我解决了这个问题。我使用的是
import org.springframework.web.portlet.ModelAndView
。
我把它改为
import org.springframework.web.servlet.ModelAndView
你在尝试什么URL,你的名字是什么?如果要将其作为WAR部署到容器中,则通常必须将其作为URI的第一部分。如果您正在使用嵌入式Tomcat、Jetty等进行部署,那么情况就不同了,但由于您有一个web.xml,我认为您没有。尝试更改它,它正在工作,我测试了您的应用程序,唯一缺少的是您有一个用于演示的视图解析器(您需要一个文件夹jsp,在这个路径中(/web-INF/jsp/demo.jsp).如果你有它,那么它应该可以工作。你只需要访问该页面
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<a href="hi.htm">Call controller</a>
@Controller
public class MyController {
@RequestMapping("/hi")
public ModelAndView add(){
System.out.println("in");
return new ModelAndView("demo", "message", "Add method called");
}
}