0

I have a small issue in request mapping,

I have controllerA with request mapping as

@Controller
@RequestMapping(value={"login/formA.html", "B/formB.html", "C/formC.html"})
public class ControllerA {
} 

I need to create one more controller without disturbing the existing controller,

request mapping for secound controller like below

@Controller
@RequestMapping(value={"X/test1", "Y/test2", "Z/test3"})
public class ControllerB {
}

How can I configure the spring-servlet.xml to configure the above scenario and to make it work.

I have the web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="https://java.sun.com/xml/ns/j2ee" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://java.sun.com/xml/ns/j2ee https://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
        <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/public/login/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/public/signout/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/public/X/*</url-pattern>
    </servlet-mapping>

</web-app>     

My spring-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"
    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">

    <mvc:annotation-driven/>

    <context:component-scan
        base-package="com.abc.xyz.controller"/>

    <context:component-scan
        base-package="com.abc.xyz.controller.example1"/>    

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

    <bean class='org.springframework.web.servlet.view.ContentNegotiatingViewResolver' p:order='1'>
            <property name='mediaTypes'>
                  <map>
                        <entry key='json' value='application/json' />
                        <entry key='html' value='text/html' />
                  </map>
            </property>

            <property name='viewResolvers'>
                  <list>
                        <bean class='org.springframework.web.servlet.view.BeanNameViewResolver' />
                        <bean class='org.springframework.web.servlet.view.UrlBasedViewResolver'>
                              <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                              <property name="prefix" value="/WEB-INF/jsp/"/>
                              <property name="suffix" value=".jsp"/>
                        </bean>
                  </list>
            </property>


      </bean>

Plese do give me suggestion to resolve this issue , struck with few days.

Thanks in Advance.

2

1 Answer 1

0

Add one more servlet mapping in web.xml like given below

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/public/*/*</url-pattern>
</servlet-mapping>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you krish for the response. But i have to map the directory also because , some scenarious like if login1/login, signout/signout two mappings then if you try the above configuration even if we make a call like login1/signout , i think the request will work, i dont want that behaviour , if the directory in web.xml mathces the directory in requestmapping in controller then only that called method need to invoke. For ex: web.xml, url-pattern is /public/view1 /* and /secured/showmessage/* and the request mapping like view1/viewService and showmessage/messageDispathcer, i hope got scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.