When we do web development based on the spring framework, we usually have the DAO layer (Database Operations), the service layer (business logic operations, such as determining whether to find data in cache such as redis,memcached, when to call the DAO layer), the Web layer (receiving requests from front-end users, Call the service layer and respond to the front end with data and views). Among them, Spring framework has done a good support for service layer, MyBatis, hibernate and other frameworks have made good support for DAO layer, and SPRING-MVC, Struts2 and so on have made good support for Web layer. Generally speaking, the Web project based on Spring Framework has SSM (Spring,spring-mvc,mybatis) system, SSH (Spring,spring-mvc,hibernate) system and so on. I'm here to demonstrate how SPRING-MVC works under the code. Let's look at the diagram of the SPRING-MVC framework:
First, we're going to add SPRING-MVC's jar dependencies to the Web project, and we'll develop the project on that basis. The case file package structure for this project is as follows:
Next, configure the Web. xml file:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://xmlns.jcp.org/xml/ns/javaee/http Xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "version=" 3.1 "> <!--Register filters to avoid Chinese garbled--<filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.characterenc Odingfilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-map ping> <!--Register Front Controller--<servlet> <servlet-name>spring-mvc</servlet-name> < Servlet-class>org.springframework.wEb.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfigloc Ation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init -param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> & Lt;servlet-name>spring-mvc</servlet-name> <url-pattern>*.html</url-pattern> </servlet-ma Pping></web-app>
Re-configure the Springmvc-config.xml file:
<?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.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd "> <!--Configuring the page Controller/processor with annotations--<context:component-scan base-package=" Com.itszt.controller "></context:component-scan> < annotations for!--addressing and controller processing support--<bean class=" Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping "></bean> <bean class= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" ></bean> <!--configuration View Resolver--<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/web-inf/" ></property> <property name= "suffix" value= ". JSP" &G T;</property> </bean></beans>
The
Background Java code is as follows:
Package com.itszt.domain;/** * Entity class, dog name, Color */public class Dog {private String dogname; Private String Dogcolor; Public String Getdogname () {return dogname; } @Override Public String toString () {return ' dog{' + ' dogname= ' + dogname + ' \ ' + ", dogcolor= '" + dogcolor + ' \ ' + '} '; } public void Setdogname (String dogname) {this.dogname = Dogname; } public String Getdogcolor () {return dogcolor; } public void Setdogcolor (String dogcolor) {this.dogcolor = Dogcolor; }}----------------------------------------------Package com.itszt.domain;import java.util.arrays;/** * Entity class, the user's name, Age, Hobby, pet dog */public class User {private String username; Private Integer userage; Private string[] Userhbs; Private dog Dog; Public String GetUserName () {return username; } @Override Public String toString () {return "user{" + "username= '" + uSername + ' \ ' + ', userage= "+ Userage +", userhbs= "+ arrays.tostring (USERHBS) + ", dog=" + Dog + '} '; } public void Setusername (String username) {this.username = username; } public Integer Getuserage () {return userage; } public void Setuserage (Integer userage) {this.userage = Userage; } public string[] Getuserhbs () {return userhbs; } public void Setuserhbs (string[] userhbs) {This.userhbs = Userhbs; } public Dog Getdog () {return dog; } public void Setdog (dog dog) {this.dog = dog; }}---------------------------------------------------------Package Com.itszt.controller;import Com.itszt.domain.user;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpsession;/** * Controller */@Controller @requestmapping ("/orgcenter ") public class Orgcontroller {@RequestMapping ("/add ") public String Add (user user, HttpServletRequest req Uest, HttpSession session) {System.out.println ("Orgcontroller.add"); SYSTEM.OUT.PRINTLN ("Receive parameter user=" +user); Request.setattribute ("user", user); StringBuffer sbf=new StringBuffer (); For (String S:user.getuserhbs ()) {Sbf.append (s+ ","); } String userhbs=sbf.tostring (); Request.setattribute ("Userhbs", Userhbs.substring (0,userhbs.length ()-1)); Request.setattribute ("Dog", User.getdog ()); return "result"; }}
The
Front-end index.jsp page code looks like this:
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>The server-side Orgcontroller response page is as follows:
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>
Hope to help you!
The implementation process of SPRING-MVC framework