Spring 4 : How to map RequestMapping URLs to particular controller

1.6k views Asked by At

I have written a Spring MVC application with multiple controllers.

On the JSP, I have the action on the form:

 <form id="createTableForm" method="post" name="createTable" action="${pageContext.request.contextPath}/saveTable" >

and the same action is mapped to a method in the Controller:

@Controller
public class TableController implements TableConstants {

  @RequestMapping(value="/saveTable")
  public String saveTable(HttpServletRequest request,RedirectAttributes redirectAttributes) {
  //...
  }
}

And in my web.xml:

  <context-param>
    <description>Context name of the Application</description>
    <param-name>contextName</param-name>
    <param-value>W****</param-value>
</context-param>
<context-param>
    <description>Database used for</description>
    <param-name>databaseName</param-name>
    <param-value>w*****</param-value>
</context-param>

<welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
</welcome-file-list>

<filter>
    <filter-name>FilterChainProxy</filter-name>
    <filter-class>com.abc.w****.configuration.FilterChainProxy
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>FilterChainProxy</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<session-config>
    <session-timeout>1</session-timeout>
</session-config>


<jsp-config>
    <taglib>
        <taglib-uri>http://displaytag.sf.net</taglib-uri>
        <taglib-location>/WEB-INF/tld/displaytag.tld</taglib-location>
    </taglib>
</jsp-config>

Do I need to include the URL mapping to that particular Controller in the web.xml file or in the WebAppConfig class?

I have the WebAppConfig annotated with @Configuration, @ComponentScan and @EnableWebMVC. It has the following methods:

 public UrlBasedViewResolver setupViewResolver() {
 }
 public MessageSource messageSource() {
 }
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
 }
 public CommonsMultipartResolver multipartResolver() { 
 }

Please advise.

2

There are 2 answers

0
igreenfield On

The @RequestMapping annotation could be applied to the controller class. In this case all the methods in this class will derive the defaults from the class annotation and the implementation could override it.

1
vijaysdey88 On

There are couple things you would need to do

  1. In web.xml configure Spring's DispatcherServlet to intercept the request and direct it to the Controllers.For example to map DispatcherServlet to requests that at root, add following lines

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    
  2. Make sure in the bean definition xml, you have configured component scan on the package that has your controllers so that Spring container can discover them.