首页 > 代码库 > 基于xml配置的springMVC-快速入门
基于xml配置的springMVC-快速入门
一:准备工作
将需要用到的spring包放进lib文件夹内,具体需要什么包自己网上查,或在我的例子里找。
二:配置文件:web.xml
主要是配置servlet路径方式和指定上下文xml文件。
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 4 5 <display-name>springMvcTest</display-name> 6 7 8 9 <servlet>10 11 <servlet-name>spmvc</servlet-name>12 13 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>14 15 /*16 17 <init-param>18 19 <param-name>contextConfigLocation</param-name>20 21 <param-value>classpath*:/springMVC.xml</param-value>22 23 </init-param>24 25 */26 27 <load-on-startup>1</load-on-startup>28 29 </servlet>30 31 32 33 <servlet-mapping>34 35 <servlet-name>spmvc</servlet-name>36 37 <url-pattern>*.do</url-pattern>38 39 </servlet-mapping>40 41 42 43 <!-- 字符集乱码,添加字符集过滤器,只能拦截post请求,get请求中文需要手动转换 -->44 45 <filter>46 47 <filter-name>encodingFilter</filter-name>48 49 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>50 51 <init-param>52 53 <param-name>encoding</param-name>54 55 <param-value>UTF-8</param-value>56 57 </init-param>58 59 <init-param>60 61 <param-name>forceEncoding</param-name>62 63 <param-value>true</param-value>64 65 </init-param>66 67 </filter>68 69 <filter-mapping>70 71 <filter-name>encodingFilter</filter-name>72 73 <url-pattern>/*</url-pattern>74 75 </filter-mapping>76 77 78 79 <welcome-file-list>80 81 <welcome-file>index.html</welcome-file>82 83 <welcome-file>index.htm</welcome-file>84 85 <welcome-file>index.jsp</welcome-file>86 87 <welcome-file>default.html</welcome-file>88 89 <welcome-file>default.htm</welcome-file>90 91 <welcome-file>default.jsp</welcome-file>92 93 </welcome-file-list>94 95 </web-app>
三:spring配置文件 spmvc-xml-servlet.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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" > <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/index.do">startControl</prop> <prop key="/hello.do">startControl</prop> <prop key="/login.do">loginController</prop><!--直接跳转方式 --> <!--//commandController方式 <prop key="/add_emp.do">loginController</prop> <prop key="/add.do">addEmpControl</prop> --> <!-- FormController方式 --> <prop key="/add_emp.do">addEmpFormController</prop> <prop key="/multi.do">bController</prop> <!-- 定义访问multi路径1 --> <prop key="/doSearch.do">bController</prop> <!-- 定义访问multi路径2 --> </props> </property> </bean> <bean id="startControl" class="com.zzk.Controller"></bean> <bean id="helloControl" class="com.zzk.helloController"></bean> <bean id="addEmpControl" class="com.zzk.EmpCommandController"> <property name="commandClass" value="com.zzk.EMP"></property> </bean> <bean id="addEmpFormController" class="com.zzk.EmpFormController"> <property name="commandClass" value="com.zzk.EMP"></property> <!-- 指定类 --> <property name="formView" value="add_emp"></property> <!-- 进行填写表单的页面 --> <property name="successView" value="success"></property><!-- 成功页面 --> </bean> <!-- 定义Multicontroller --> <bean id="bController" class="com.zzk.EmpMultiController"> <property name="methodNameResolver"> <ref local="bMethodNameResolver"/> <!-- 使用这种解析方式 --> </property> </bean> <!-- 请求路径:http://localhost:8080/springMvcTest/multi.do?method=doSearch --> <bean id="bMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="method"></property> <property name="logicalMappings"> <props> <prop key="doSearch">doSearch</prop> </props> </property> </bean> <!-- 请求路径:http://localhost:8080/springMvcTest/doSearch.do ;key值需要与方法名相同,不包括.do--> <bean id="cMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver"></bean> <!-- 第三种很少用,不做解释了 --> <bean id="dMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <value> /sb/page.html=doSearch </value> </property> </bean> <!-- 用于逻辑视图到真正视图的隐射 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 这里是用于直接跳转的类UrlFilenameViewController --> <bean id="loginController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/></beans>
四:配置文件解析
最简单访问形式:url:http://localhost:8080/springMvcTest/index.do
1.SimpleUrlHandlerMapping简单路径映射类,比较常用,接收路径请求。
1 <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 2 3 <property name="mappings"> 4 5 <props> 6 7 <prop key="/index.do">startControl</prop> //通过这里找到id为startControl的bean 8 9 </props>10 11 </property> 12 13 </bean>
2.<bean id="startControl" class="com.zzk.Controller"></bean> //找到这个id,并指向这个类文件。
3.Controller.class
1 package com.zzk; 2 3 4 5 import javax.servlet.http.HttpServletRequest; 6 7 import javax.servlet.http.HttpServletResponse; 8 9 import org.springframework.web.servlet.ModelAndView;10 11 12 13 public class Controller implements org.springframework.web.servlet.mvc.Controller{ 14 15 @Override16 17 public ModelAndView handleRequest(HttpServletRequest arg0,18 19 HttpServletResponse arg1) throws Exception {20 21 // TODO Auto-generated method stub22 23 ModelAndView mav = new ModelAndView("hello"); //这个类是用来保存view和返回信息的。24 25 mav.addObject("message", "Hello World!"); 26 27 return mav; //返回给hello.jsp这个视图28 29 }30 31 }
4. InternalResourceViewResolver
<!-- 用于逻辑视图到真正视图的隐射 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean>
5.hello.jsp
<%@ page language="java" contentType="text/html;utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><script type="text/javascript" src="WebContent/js/text.js"></script><head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>ddd${message }<img src="images/53.gif"/></body></html>
五:执行流程
url地址发送请求 –> 通过web.xml判断是否为 .do结尾的路径 -> 进入spring配置文件 -> 找到SimpleUrlHandlerMapping这个类的配置 -> 通过配置找到请求的key -> 再找到这个key 的值的对应id名的bean配置 -> 通过这个bean,进入controller类进行处理 -> 返回一个视图(“hello”) -> 回到spring配置文件,找到InternalResourceViewResolver这个配置 –> 并按它的要求找到对应的jsp文件 -> 响应。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。