标签:
SpingMVC 是一个MVC框架,通过实现MVC很好的将数据、业务、展现进行分离
一、MVC架构
二、Spring MVC架构
架构流程:
1、 用户发送请求至前端控制器DispatcherServlet
2、 DispatcherServlet收到请求调用HandlerMapping处理器映射器。
3、 处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet。
4、 DispatcherServlet通过HandlerAdapter处理器适配器调用处理器
5、 执行处理器(Controller,也叫后端控制器)。
6、 Controller执行完成返回ModelAndView
7、 HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet
8、 DispatcherServlet将ModelAndView传给ViewReslover视图解析器
9、 ViewReslover解析后返回具体View
10、 DispatcherServlet对View进行渲染视图(即将模型数据填充至视图中)。
11、 DispatcherServlet响应用户
三、入门例子
1、创建工程,拷贝SpringMVC相关的jar包
2、配置web.xml,配置中央控制器DispatherServlet
<servlet> <servlet-name>springmvc01</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc01</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>3、在创建springmvc配置文件,配置映射器,视图解析器
<!-- handler--> <bean name="/hello.do" class="com.wangfan.spingmvc01.HelloController"></bean> <!-- 映射器 --> <bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean> <!-- 适配器 --> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/view/"></property> <!-- 后缀 --> <property name="suffix" value=".jsp"></property> </bean>4、创建首页
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <form action="hello.do" method="post"> hello:<input type="text" name="hello" /> <input type="submit" value="提交" /> </form> </body> </html>
5、创建controller继承AbstractController,重写handleRequestInternal
package com.wangfan.spingmvc01; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { String hello=request.getParameter("hello"); System.out.println("----:"+hello); ModelAndView mav=new ModelAndView("index"); mav.addObject("helloworld", "hello"+hello); return mav; } }
6、创建需要发现请求的JSP页面
/view目录下
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <h1>${helloworld}</h1> </body> </html>
四、映射器三种方式
<bean id="userController" name="/user.do" class="com.wangfan.springmvc03.UserController"></bean> <!-- 配置映射器HandlerMapping --> <!-- 第一种方式:根据beanname找到对应的controller --> <!-- <bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean> --> <!-- 第二种方式:根据简单URL来找到对应的controller --> <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/userInfo.do">userController</prop> </props> </property> </bean> --> <!-- 第三种方式:控制类的类名访问找到对应的controller ,访问时首字母需要小些 --> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> <bean class="com.wangfan.springmvc03.UserController"></bean>
1、首页
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <a href="user/list.do">查询</a><br> <a href="user/add.do">添加</a><br> <a href="user/update.do">修改</a><br> </body> </html>
user_add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <h1>添加用户信息</h1> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springmvc04</display-name> <servlet> <servlet-name>springmvc04</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc04</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- spring注解驱动 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 扫描器 --> <context:component-scan base-package="com.wangfan.springmvc04"></context:component-scan> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/view/"></property> <!-- 后缀 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
package com.wangfan.springmvc04; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/user")//User Controller模块 public class UserController { @RequestMapping(value = "/list.do") // 默认post和get方式都行 public String list() { System.out.println("----查询用户信息-----"); return "userinfo/user_list"; } @RequestMapping("/add.do") public String add(){ System.out.println("----添加用户信息-----"); return "userinfo/user_add"; } @RequestMapping("/update.do") public String update(){ System.out.println("----修改用户信息-----"); return "userinfo/user_update"; } }
六、Spring结合Ajax以Json格式传递数据
前端:
配置SpringMVC.xml把对象解析成Json格式:
后端Controller:
标签:
原文地址:http://blog.csdn.net/wust__wangfan/article/details/51874270