springMVC框架搭建流程

来源:互联网 发布:php 请求参数加密 编辑:程序博客网 时间:2024/06/05 03:29

1、创建Dynamic Web Project

2、导入spring和springmvc所需要的文件

3、配置web.xml文件

3.1 监听spring上下文容器

3.2 加载spring的xml文件到spring的上下文容器(spring-context.xml)

3.3 配置spring MVC的DispatcherServlet

3.4 加载spring MVC的xml到spring的上下文容器(springMVC-context.xml)

3.5 配置DispatcherServlet所需要拦截的 url(固定了HTTP的格式 如*.do)

4、配置spring的xml文件

主要配置链接数据库等信息

5、配置spring MVC的xml文件

5.1 加载spring的全局配置文件

5.2 扫描指定包下的所有类是注解生效

5.3 配置SpringMVC的视图渲染器

6、写Controller(TestController.java)

6.1 @Controller

6.2 @RequestMapping()

7、写jsp文件


执行流程个人总结:接收到请求后会扫描springMVC配置文件中指定的包中的类(controller),根据controller中的注解@RequestMapping("test")找到对应的jsp文件。


web.xml文件

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name></display-name><!--1.  监听spring上下文容器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 2. 加载spring的xml到spring的上下文容器 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-context.xml</param-value></context-param><!-- 3. 配置spring MVC的DispatcherServlet --><servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--4.  加载spring MVC的xml到spring的上下文容器 --><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/springMVC-context.xml</param-value></init-param><!-- 启动加载该servlet --><load-on-startup>1</load-on-startup></servlet><!--5. 配置DispatcherServlet所需要拦截的 url --><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping></web-app>


spring-context.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:context="http://www.springframework.org/schema/context"      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">      <!-- Root Context: defines shared resources visible to all other web components -->           </beans>  

springMVC-context.xml

<?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 加载Spring的全局配置文件 --><beans:import resource="spring-context.xml" /><!-- SpringMVC配置 --><!-- 通过component-scan 让Spring扫描org.swinglife.controller下的所有的类,让Spring的代码注解生效 --><context:component-scan base-package="com.liuyunlong.controller"></context:component-scan><!-- 配置SpringMVC的视图渲染器, 让其前缀为:/ 后缀为.jsp  将视图渲染到/page/<method返回值>.jsp中 --><beans:beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"p:prefix="/page/" p:suffix=".jsp"></beans:bean></beans:beans>


TestController.java

package com.liuyunlong.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class TestController {@RequestMapping("test")public String test(){return "test";}}

jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'test.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    Welcome to springMVC <br>  </body></html>







0 0