Why do you learn SPRINGMVC?
The spring framework claims to be a one-stop solution for Java EE applications, and spring itself provides a well-designed MVC framework, SPRINGMVC. Because of the high market share of the spring framework, more and more spring framework users use SPRINGMVC instead of the former Struts2 of the MVC framework. Of course spring can also seamlessly integrate struts, JSF and other excellent MVC frameworks.
More and more enterprises began to choose Springmvc+mybatis to build system architecture, and in e-commerce hot today, Springmvc+mybatis has become the best combination of e-commerce project architecture.
What is MVC thinking? What are the advantages of MVC? MVC, which is not a unique design idea of the Java language, is not a unique idea for Web applications, but a specification that all object-oriented programming languages should adhere to. The advantage of MVC is that:
He put an application at a cost of three levels (M,v,c not much here), these three parts work together to minimize the coupling of application code, which improves the scalability and maintainability of the program.
Multiple views can correspond to a model, which reduces the duplication of code and the amount of code maintenance.
Because the model data and the display layer are separated, the model data can be applied to any display technology, including JSP pages, velocity templates, or direct production of Excel documents.
MVC is more conducive to engineering management software, different layers of their own work, in favor of engineering and tools to manage the program code.
What are the advantages of SPRINGMVC?
The SPRINGMVC is highly flexible, non-intrusive, and configurable.
The SPRINGMVC provides a front-end controller dispatcherservlet that developers do not have to develop additional controller objects.
SPRINGMVC Division of labor is clear, including controller, validator, command object, model object, handler mapping attempt parser, and so on, each function is implemented by a special object to be responsible for the completion.
Springmvc can automatically bind user input and correctly convert the data type. (For example, you can parse a string into a model's int or float-type property).
SPRINGMVC uses a name/value map object to achieve more flexible model data transfer.
The SPRINGMVC includes a common validator that verifies user input and redirects to an input form if the checksum does not pass. The input checksum is optional and supports both programmatic and declarative methods.
SPRINGMVC support internationalization, configuration is extremely simple.
SPRINGMVC supports multiple view technologies: JSP, Velocity, and freemarker.
Spring provides a simple and powerful JSP tag library that supports data binding capabilities, making it easier to write JSP pages.
Not much to say, directly on the code (the steps to write HelloWorld):
The first thing to do is to import the required jar packages: All of Spring's jar packages and Commons-logging.jar.
spring:http://repo.spring.io/release/org/springframework/spring/.
Commons-logging.jar:http:commons.apache.org.
Configure the configuration of the Web.xml--dispatcherservlet front-end controller.
<!-- definition SPRING&NBSP;MVC Front Controller --> <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> <!-- Let SPRING&NBSP;MVC's front-end controller intercept all requests --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Configure SPRINGMVC controller<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http// Www.springframework.org/schema/beans "xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd " ><!-- Configure handle, map Hello request --><bean name= "/hello" class= " Org.fkit.controller.HelloController "/><!-- processing mapper looks up the name of the bean as a URL and needs to specify name (that is, url) when configuring handle - -><bean class= "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/><!-- Simplecontrollerhandleradapter is a processor adapter, all processor adapters are to implement the Handleradapter interface --><bean class= " Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter "/><!-- View Resolver --><bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"/></beans> Implementation of Controller class
public class Hellocontroller implements controller{private static final Log logger = Logfactory.getlog ( Hellocontroller.class); @Overridepublic modelandview HandleRequest (httpservletrequest arg0, HttpServletResponse arg1 ) throws Exception {//TODO auto-generated method Stublogger.info ("HandleRequest is called"); Modelandview mv = new Modelandview (); Mv.addobject ("Message", "Hello springmvc!"); Mv.setviewname ("/web-inf/content/welcome.jsp"); return MV;}}
Implementation of the View page
<! DOCTYPE html>
Test app: Visit the following URL:Http://localhost:8080/SpringMVCTest/hello
This article is from the "Five Piglets" blog, please be sure to keep this source http://wuxiaozhu.blog.51cto.com/7942143/1916348
SPRINGMVC Introduction Demo