在DispatcherServlet中找不到带有URI的HTTP请求的映射,名称为“dispatcher”

时间:2011-07-18 12:23:57

标签: spring netbeans

我是Spring框架的新手。我刚刚开始在netbeans中实现多路操作控制器。但。我收到了上述错误。我在下面粘贴我的代码。 Plz看看它并解决我的问题。

调度-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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

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

    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />
    <bean name="/*.htm" class="controller.MyController"/>
</beans>

index.jsp:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Hello</title>
</head>

<body>
    <h4>Multi Action Controller Example</h4>
    <a href="add.htm">Add</a>
    <a href="update.htm">Update</a>
    <a href="remove.htm">Remove</a>
</body>
</html>

MyController.java:

package controller; 

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class MyController extends MultiActionController {

    public ModelAndView add(HttpServletRequest req, HttpServletResponse resp) throws      Exception {
        System.out.println("Add ma");
        return new ModelAndView("result","message","Add Method Called");
    }

    public ModelAndView update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("Update ma");
        return new ModelAndView("result","message","Update Method Called");
    }

    public ModelAndView remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("Remove ma");
        return new ModelAndView("result","message","Remove Method Called");
    }
}

result.jsp:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Nee Varaatha</title>
</head>
<body>
    <h1>Please Show it </h1>
    ${message}
</body>
</html>

2 个答案:

答案 0 :(得分:1)

尝试在netbeans的输出窗口中检查您的apachelog,看看控制器中的默认映射是什么,并将该映射附加到index.jsp

例如,您可以在日志中找到

  

将映射的URL路径[/ employee]映射到处理程序'/ * .html'

只需在JSP

中附加employee / add.htm即可

答案 1 :(得分:0)

您是否考虑过使用@Controller注释类?由于弹簧2.5注释通常是优选的。

以下是web.xml示例:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" 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_5.xsd">
<display-name>Example</display-name>



<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/example-servlet.xml        
    </param-value>

</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</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"
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.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">


<!--Tell the servlet where to look for annotated methods-->
<context:component-scan base-package="controller" />

<!--if no controller logic is required, mvc:view-controller can be used to simply show a view for a request  -->
<mvc:view-controller path="/" view-name="index"/>

<!--Enables many annotations and searches for @Controller annotated methods etc.. -->
<context:annotation-config />

<!--JSR-303 (Bean validation) support will be detected on classpath and enabled automatically-->
<mvc:annotation-driven />

<!--This tag allows for mapping the DispatcherServlet to "/" (all extensions etc)-->
<mvc:default-servlet-handler/>

<mvc:resources location="/resources/**, classpath:resources" mapping="/resources/**"/>

<!--Configures the application to search for views in folder /WEB-INF/jsp/ with the suffix ".jsp" 
in controllers prefix and suffix are therefore no longer needed-->  
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> 

一个简单的控制器示例:

@Controller
public class ExampleController {

    @RequestMapping("/test")
    public String test(Model model) {
            model.addAttribute("message","Test message");
            return "result";
    }
}

另请参阅弹簧参考文档here