To configure annotation mapping use the below code in dispatcher-servlet.xml
<context:component-scan base-package="com.candidjava.springmvc"></context:component-scan>
Now annotate your controller with @Controller and @RequestMapping to map your url with controller
@RequestMapping(value = "/customer.htm") @Controller
Also you can mention the request type is GET or POST using
@RequestMapping(method = RequestMethod.GET)
Complete Code
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: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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.candidjava.springmvc"></context:component-scan> <!-- ************************************************************* --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
CustomerController.java
package com.candidjava.springmvc; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @RequestMapping(value = "/customer.htm") @Controller public class CustomerController { @RequestMapping(method = RequestMethod.GET) public ModelAndView addCustomer(HttpServletRequest request, HttpServletResponse response) { ModelAndView model = new ModelAndView("page"); model.addObject("msg", "Action Page is Get..!"); return model; } @RequestMapping(method = RequestMethod.POST) public ModelAndView editCustomer(HttpServletRequest request, HttpServletResponse response) { ModelAndView model = new ModelAndView("page"); model.addObject("msg", "Action Page is Post..!"); return model; } }
Screenshot
Download