user3403462
user3403462

Reputation: 121

Spring 4 : How to map RequestMapping URLs to particular controller

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.

Upvotes: 6

Views: 1614

Answers (2)

vijaysdey88
vijaysdey88

Reputation: 11

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.

Upvotes: 0

igreenfield
igreenfield

Reputation: 1702

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.

Upvotes: 1

Related Questions