Following previous article, "Spring MVC HelloWorld example Part 1
In this part, I am going to show example on how to define Handler Mapping in our xml setting file.
By Default, SpringMVC is using "BeanNameUrlHandlerMapping" as HandlerMapping,
there are two other HandlerMapping for our choices.
2. ControllerClassNameHandlerMapping
3. SimpleUrlHandlerMapping.
The only two differences would be in "HelloWeb-servlet.xml" and our Controller class
I have marked the line of codes that is different from previous example.
1. HelloWeb-servlet.xml
<beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 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"> <bean name ="ControllerClassNameHandler" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> <bean name="/helloworld" class = "com.yang.HelloController"></bean> <bean id ="viewresolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name = "prefix" value = "/WEB-INF/jsp/" /> <property name = "suffix" value = ".jsp" /> </bean> </beans>
2. Controller Class
package com.yang; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class HelloController extends AbstractController{ @Override protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { // TODO Auto-generated method stub ModelAndView mv = new ModelAndView(); mv.addObject("message", "Hello Spring MVC Framework!"); mv.setViewName("success"); return mv; } }
The Third way to define Controller Class
In below example, I have defined a class UserModel class,
in this Model, it contains variable Username and UserAge.
And in the controller, I will pass this Model class to send to the View.
1. Simple UserModel class
package com.yang.model; public class UserModel { private String Username; private int UserAge; public String getUsername() { return Username; } public void setUsername(String username) { Username = username; } public int getUserAge() { return UserAge; } public void setUserAge(int userAge) { UserAge = userAge; } }
2. MyCommandController
package com.yang; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import com.yang.model.UserModel; public class MyCommandController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { // TODO Auto-generated method stub ModelAndView mv = new ModelAndView(); UserModel us = new UserModel(); us.setUserAge(18); us.setUsername("Yang"); mv.addObject("message", us); mv.setViewName("user"); return mv; } }