开发者

No mapping found for HTTP request with URI in DispatcherServlet with name 'dispatcher'

I'm new to Spring fra开发者_开发百科mework. I just started implementing multiaction controller in netbeans. But. I'm getting the above error. I'm pasting my code below. Plz take a look into it and resolve me the issue.

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


Try checking your apachelog in the output Window of netbeans and see what's the default mapping of your controller in it, and append that mapping in the index.jsp.

For example in the log you'll find

Mapped URL path [/employee] onto handler '/*.html'

Simply append employee/add.htm in the JSP


Have you considered using @Controller annotated classes? Since spring 2.5 annotations are usually preferred.

Here's an example 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>

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

And an example simple controller:

@Controller
public class ExampleController {

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

Also see the spring reference documentation here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜