Spring MVC屬於SpringFrameWork的後續產品,已經融合在Spring Web Flow裏面。Spring 框架提供了構建 Web 應用程序的全功能 MVC 模塊。使用 Spring 可插入的 MVC 架構,能夠選擇是使用內置的 Spring Web 框架還能夠是 Struts 這樣的 Web 框架。javascript
Spring 框架提供了構建 Web 應用程序的全功能 MVC 模塊。 使用 Spring 可插入的 MVC 架構,能夠選擇是使用內置的 Spring Web 框架仍是 Struts 這樣的 Web 框架。經過策略接口,Spring 框架是高度可配置的,並且包含多種視圖技術,例如 JavaServer Pages(JSP)技術、Velocity、Tiles、iText 和 POI。Spring MVC 框架並不知道使用的視圖,因此不會強迫您只使用 JSP 技術。Spring MVC 分離了控制器、模型對象、分派器以及處理程序對象的角色,這種分離讓它們更容易進行定製。html
優勢介紹java
Lifecycle for overriding binding, validation, etc.;易於同其它View框架(Titles等)無縫集成,採用IOC便於測試。jquery
它是一個典型的教科書式的mvc構架,而不像struts等都是變種或者不是徹底基於mvc系統的框架,對於初學者或者想了解mvc的人來講我以爲 spring是最好的,它的實現就是教科書!第二它和tapestry同樣是一個純正的servlet系統,這也是它和tapestry相比 struts所沒有的優點。並且框架自己有代碼,並且看起來也不費勁比較簡單能夠理解。web
applicationContext.xmajax
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--<mvc:default-servlet-handler/>--> <!-- <mvc:resources mapping="/image/" location="/image/**"></mvc:resources>--> <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>--> <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/h1.do">firstController</prop> <prop key="/h2.do">firstController</prop> </props> </property> </bean>--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/*.do" value="firstController"></entry> </map> </property> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsps/"></property> <property name="suffix" value=".jsp"></property> </bean> <bean id="firstController" class="cn.curry.controller.MyController"></bean> </beans>
MyControllerspring
public class MyController implements Controller { public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mav = new ModelAndView(); mav.addObject("msg", "呵呵第一個SpringMVC"); mav.setViewName("index"); return mav; }
web.xmljson
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.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> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <!--歡迎頁--> <welcome-file-list> <welcome-file>/WEB-INF/jsps/index.jsp</welcome-file> </welcome-file-list> </web-app>
實例二spring-mvc
applicationContext.xml架構
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--<mvc:default-servlet-handler/>--> <!--<mvc:resources mapping="/image/" location="/image/**"></mvc:resources>--> <!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>--> <!--<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/my.do">firstController</prop> </props> </property> </bean>--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/*.do" value="firstController"></entry> </map> </property> </bean> <bean id="nameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/h1.do">doFirst</prop> <prop key="/h2.do">doSecond</prop> </props> </property> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsps/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- <bean id="hi.do" class="cn.curry.controller.MyController"> <property name="supportedMethods" value="POST,GET"></property> </bean>--> <bean id="firstController" class="cn.curry.controller.MyMultiActionController"> <property name="methodNameResolver" ref="nameResolver"></property> </bean> </beans>
MyMultiActionController
package cn.curry.controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by Curry on 2017/4/10. */ public class MyMultiActionController extends MultiActionController { public ModelAndView doFirst(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mav = new ModelAndView(); mav.addObject("msg", "呵呵第一個SpringMVC"); mav.setViewName("index"); return mav; } public ModelAndView doSecond(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mav = new ModelAndView(); mav.addObject("msg", "呵呵第二個SpringMVC"); mav.setViewName("index"); return mav; } }
實例三
application.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--<mvc:default-servlet-handler/>--> <!--<mvc:resources mapping="/image/" location="/image/**"></mvc:resources>--> <!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>--> <!--<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/my.do">firstController</prop> </props> </property> </bean>--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/*.do" value="firstController"></entry> </map> </property> </bean> <bean id="nameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/h1.do">doFirst</prop> <prop key="/h2.do">doSecond</prop> </props> </property> </bean> <!--<bean id="nameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="actionName"></property> </bean>--> <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsps/"></property> <property name="suffix" value=".jsp"></property> </bean>--> <!--<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <bean id="baidu" class="org.springframework.web.servlet.view.RedirectView"> <property name="url" value="http://www.baidu.com"></property> </bean> <bean id="keke" class="org.springframework.web.servlet.view.JstlView"> <property name="url" value="/WEB-INF/jsps/index.jsp"></property> </bean>--> <bean class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="location" value="classpath:myView.xml"></property> </bean> <bean id="firstController" class="cn.curry.controller.MyMultiActionController"> <property name="methodNameResolver" ref="nameResolver"></property> </bean> </beans>
myView.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>--> <bean id="baidu" class="org.springframework.web.servlet.view.RedirectView"> <property name="url" value="http://www.22ddkk.cn"></property> </bean> <bean id="keke" class="org.springframework.web.servlet.view.JstlView"> <property name="url" value="/WEB-INF/jsps/index.jsp"></property> </bean> </beans>
實例四
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="cn.curry"></context:component-scan> <!--<mvc:default-servlet-handler/>--> <!--<mvc:resources mapping="/image/" location="/image/**"></mvc:resources>--> <!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>--> <!--<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/my.do">firstController</prop> </props> </property> </bean>--> <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/*.do" value="firstController"></entry> </map> </property> </bean>--> <!-- <bean id="nameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/h1.do">doFirst</prop> <prop key="/h2.do">doSecond</prop> </props> </property> </bean>--> <!--<bean id="nameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="actionName"></property> </bean>--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsps/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <bean id="baidu" class="org.springframework.web.servlet.view.RedirectView"> <property name="url" value="http://www.baidu.com"></property> </bean> <bean id="keke" class="org.springframework.web.servlet.view.JstlView"> <property name="url" value="/WEB-INF/jsps/index.jsp"></property> </bean>--> <!-- <bean class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="location" value="classpath:myView.xml"></property> </bean>--> <!-- <bean id="firstController" class="MyMultiActionController"> <property name="methodNameResolver" ref="nameResolver"></property> </bean>--> </beans>
package cn.curry.entity; /** * Created by Curry on 2017/4/11. */ public class Users { private Integer uids; private String uname; private Adress adress; public Integer getUids() { return uids; } public void setUids(Integer uids) { this.uids = uids; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public Adress getAdress() { return adress; } public void setAdress(Adress adress) { this.adress = adress; } }
package cn.curry.entity; /** * Created by Curry on 2017/4/11. */ public class Adress { private String adressss; public String getAdressss() { return adressss; } public void setAdressss(String adressss) { this.adressss = adressss; } }
package cn.curry.controller; import cn.curry.entity.Users; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; /** * Created by Curry on 2017/4/6. */ @Controller public class MyController { @RequestMapping(value = "/first",method = RequestMethod.POST) public String doFirst(Model model, @RequestParam(value="uname",required = false) String uname) { model.addAttribute("msg","註解SpringMVC111"); System.out.println(uname); model.addAttribute("uname",uname); return "index"; } @RequestMapping("/first2") public String doSecond(Model model,Users users) { model.addAttribute("msg","註解SpringMVC222"); System.out.println(users.getUname()); System.out.println(users.getAdress().getAdressss()); model.addAttribute("uname",users.getUname()); model.addAttribute("adressss",users.getAdress().getAdressss()); return "index"; } @RequestMapping(value = "/{name}/{age}/first",method = RequestMethod.POST) public String doThree(Model model,@PathVariable("rname") String uname ,@PathVariable String uage) { model.addAttribute("msg","註解SpringMVC111"); System.out.println(uname); model.addAttribute("uname",uname); model.addAttribute("uage",uage); return "index"; } }
package cn.curry.controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by Curry on 2017/4/10. */ public class MyMultiActionController extends MultiActionController { public ModelAndView doFirst(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mav = new ModelAndView(); //mav.addObject("msg", "呵呵第一個SpringMVC"); mav.setViewName("baidu"); return mav; } public ModelAndView doSecond(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mav = new ModelAndView(); mav.addObject("msg", "呵呵第二個SpringMVC"); mav.setViewName("keke"); System.out.println("hehehehe"); return mav; } }
package cn.curry.controller; import cn.curry.entity.Users; import com.alibaba.fastjson.JSON; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * Created by Curry on 2017/4/12. */ @Controller public class AjaxCpntroller { @RequestMapping(value = "/first.do") public void doFirst(HttpServletResponse response) throws Exception { Map<String,Users> map=new HashMap<String, Users>(); Users u1=new Users(); u1.setUname("開心"); u1.setUids(001); Users u2=new Users(); u2.setUname("快樂"); u2.setUids(002); map.put("01",u1); map.put("02",u2); String jsonString = JSON.toJSONString(map); System.out.println(jsonString); response.setCharacterEncoding("utf-8"); response.getWriter().write(jsonString); response.getWriter().close(); } }
package cn.curry.controller; import cn.curry.entity.Users; import com.alibaba.fastjson.JSON; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; /** * Created by Curry on 2017/4/12. */ @Controller public class AjaxCpntrollerA { @RequestMapping(value = "/second.do",produces = "text/html;charset=utf-8") @ResponseBody public String doFirst(HttpServletResponse response) throws Exception { Map<String,Users> map=new HashMap<String, Users>(); Users u1=new Users(); u1.setUname("開心"); u1.setUids(001); Users u2=new Users(); u2.setUname("快樂"); u2.setUids(002); map.put("01",u1); map.put("02",u2); String jsonString = JSON.toJSONString(map); response.setCharacterEncoding("utf-8"); return jsonString; } }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page isELIgnored="false" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <html> <head> <title>添加</title> </head> <body> <h2>哈哈${msg}</h2> <h3>${uname} ${adressss}</h3> <form method="post" action="/first2"> 用戶名<input name="uname"/> 地址<input name="adress.adressss"/> <input type="submit" value="提交"/> </form> <img src="image/asp.jpg"/> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page isELIgnored="false" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <html> <head> <title>ajax</title> <script type="text/javascript" src="js/jquery-3.1.1.js"></script> <script type="text/javascript"> $(function(){ $('#btn').click(function () { $.ajax({ url:"first.do", type:"post", success:function(data){ data=eval("("+data+")"); $.each(data,function (i,dom) { alert(dom.uname+"和"+dom.uids); var li="<li>"+dom.uname+"</li>"; var li2="<li>"+dom.uids+"</li>"; $("#aa").append(li); $("#aa").append(li2); }); } }); }); }); </script> </head> <body> <input type="button" id="btn" value="提交"/> <ul id="aa"> </ul> </body> </html>