Spring MVC: Controller class name handling mapping

The nice benefits of controller class name handling mapping are:

If the project is hello and the WelcomeController is the controller, then the access address is:

http://localhost:8080/hello/welcome

http://localhost:8080/hello/welcome.html

http://localhost:8080/hello/welcomeaaa.html

http://localhost:8080/hello/welcome([a-zA-Z*]).html infinite configuration like this

 

The corresponding class needs to be introduced

<bean        class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> 

Then add the controller class bean you want to access, such as:

<bean class="chapter2.web.controller.HelloController" />
<bean class="chapter2.web.controller.WelcomeController" />

  

Let's see an example:

WelcomeController.java

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 WelcomeController extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		ModelAndView mv = new ModelAndView("welcome");
		mv.addObject("message", "welcome spring mvc!");
		return mv;
	}

} 

Add supported spring classes in xxx-servlet.xml

<!-- Controller class name processing mapping -->     
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />  
<bean class="chapter2.web.controller.WelcomeController"/>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
<%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>welcome</title>
</head>
<body>


<h2>welcome</h2>
${message}

</body>
</html>

  

Run the project:

http://localhost:8080/hello/welcome

http://localhost:8080/hello/welcome.html

http://localhost:8080/hello/welcomeaaa.html

 

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324823732&siteId=291194637