SpringMVC processes and development steps

springMVC development steps

Note: This article is just a simple description of the steps to develop the idea, does not mean that all the details of the development are described clearly.

1. Frame of introducing springMVC

The springMVC jar package into the project


Configuring springMVC front controller in web.xml
In web.xml, add the following fragment to configure a front-end controller, which is the first step in each item springMVC first thing to do.
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>
  		org.springframework.web.servlet.DispatcherServlet
  	</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		 <param-value>/WEB-INF/springmvc-config.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>

3. Handle class definition processing requested by the user
Controller class can implement or use @Controller comment. DispatcherServlet all essential framework SpringMVC front controller, which receives all the http request, and a distribution request.
How to end after the current controller intercepts the request distribution request it? There are two ways, one way is based on the xml configuration, there is a comment added (simply used) on the implementation class, the class header added @Controller to describe a class which is inherited controller,
Use annotations @RequestMapping (value = "/ hello") hello request described method, which would allow accurate distribution request front controller.
Briefly, the DispatcherServlet request corresponding to the pretreatment, any requests to go through the controller, is achieved Controller class is equivalent to a Servlet, call the business logic background. Both achieved with the use of a centralized request, low dispersion associated service coupling.
Examples are as follows:
This is done using xml mapping request:
<bean name="/hello" class="org.fkit.controller.HelloController">
Then type in the name of the main method is to rewrite handleRequest method, and must return a ModelAndView object.

This is done using annotations mapping request:
The method may be any name, class name and a method of simply adding a corresponding annotations
@org.springframework.stereotype.Controller
public class HelloController{
	private static final Log logger=LogFactory.getLog(HelloController.class);
	
	@RequestMapping(value="/hello")
	public ModelAndView hello(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("message","Hello World!");
		mv.setViewName("/WEB-INF/content/welcome.jsp");
		return mv;
	}
}
4. Write view resources
When the handle end business processes, usually return a ModelAndView object, which should include a view or view name and model of return, the name of this view represents the physical resources required to display the view.
如果只是想将一些数据传给视图资源,则可以通过模型对象。

经过上面五个步骤,基本完成了一个SpringMVC处理流程的开发,也就是执行了一个完整的请求->响应。

以下是一个标准的SpringMVC的工作流程图:



注:本文参考自《Spring+Mybatis企业应用实战》
发布了10 篇原创文章 · 获赞 11 · 访问量 5987

Guess you like

Origin blog.csdn.net/disILLL/article/details/79601738