Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 655
user3403462 Avatar asked Jun 19 '15 12:06

user3403462


People also ask

Which component maps the URL to the controller?

The ASP.NET MVC framework uses the ASP.NET routing engine, which provides flexibility for mapping URLs to controller classes. You can define routing rules that the ASP.NET MVC framework uses in order to evaluate incoming URLs and to select the appropriate controller.

Which annotation is used to map a request to a method of a controller?

RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.

Can we use @RequestMapping with @component?

Quite right, you can only use @RequestMapping on @Controller annotated classes. From the javadoc of the @Controller class: Base Controller interface, representing a component that receives HttpServletRequest and HttpServletResponse instances just like a HttpServlet [...]


1 Answers

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.

like image 59
igreenfield Avatar answered Sep 16 '22 13:09

igreenfield