2014-03-13 82 views
1

我试图打电话给控制器的链接,但它不工作的所有注释配置和URL模式是在配置文件中所提及般。这里是我的代码春天RequestMappping不工作

调度-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: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> 

的web.xml

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

HTML:

<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"); 
    } 
} 
+0

你想要什么URL,你的WAR名称是什么?如果您以WAR的形式部署到容器,那么您通常必须将其作为URI的第一部分。如果你使用嵌入式Tomcat,Jetty等进行部署,那么这是一个不同的故事,但是由于你有一个web.xml,我认为你不是。 – CodeChimp

+0

试图改变这种Call controller vvp

+0

这是工作,我测试你的应用程序,缺少你的情况,你有视图解析器的演示(你需要一个文件夹中的JSP,在这个路径中的唯一的事(/WEB-INF/jsp/demo.jsp )。如果你有它,那么它应该工作。你只需要访问页面http:// host:port/appcontext/hi.htm –

回答

2

我解决了这个问题。我正在使用import org.springframework.web.portlet.ModelAndView。 我将其更改为import org.springframework.web.servlet.ModelAndView