springMVC框架搭建

好久不写博客了,转眼都工作一年了。而到现在,才开始想要去了解了解spring的搭建及原理。

  首先我搭建好的项目结构如图:

1、新建一个动态网站项目。

2、在WEB-INF/lib下导入jar包,关于jar包具体有哪些,具体用那些还没做研究,只是简单的copy过来了如下一坨:

然后build path。

3、在WEB-INF下写一个web.xml 

  这里共做了三件事:

      写了一个过滤器,能过滤掉乱码,保持字符集的一致,至于还能过滤啥,还过滤了啥,有待研究;

      写了一个DispatcherServlet,主要用作任务调度,相当于人的大脑吧,然后这里还要写一个springMVC-servlet.xml,<load-on-startup>1</load-on-startup>中的1代表优先级最高,第一个加载;

      写了一个spring加载配置文件的东西。

<?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"
    version="3.0">

    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/springMVC-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/page/index.html</welcome-file>
    </welcome-file-list>
    
    <!-- Spring 加载配置文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>

</web-app>

4、在WEB-INF下写一个springMVC-servlet.xml

  该文件主要是对bean工厂的配置,这里工作了四件事:

      写了一个注解映射,这样程序里就可以使用注解了;
      写了一个静态资源,静态资源可以通过过滤器,相当于一个通行证(理解可能不是很合理,待研究);
      写了一个扫描,这样系统就可以扫描指定包下的@注解;
      写了一个视图解析器,负责分配页面跳转;

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-4.2.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.2.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    <!-- 使用默认的注解映射 -->
    <mvc:annotation-driven />
    <!-- 配置静态资源 -->
    <mvc:resources location="/page/" mapping="/*" />
    <!-- 自动扫描controller包中的控制器 -->
    <context:component-scan base-package="com.gwssi.*" />
    <!-- 视图解析器 -->
    <bean id="viewReslver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/page/"></property>
        <property name="suffix" value=".html"></property>
    </bean>    
</beans>

5、然后写一个bean工厂呗,让component,controller有一个归宿。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: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.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"
    default-autowire="byName">

   
</beans>

6、然后简单写一个controller吧

package com.gwssi.zpc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/a")
public class TestController {
	
	@RequestMapping("/a")
	public String aa(){
		System.out.println("aa");
		return "aa";
		
	}

}

 懒,就这样吧,继续学习了。

 

posted @ 2018-05-30 19:31  非非是  阅读(195)  评论(0编辑  收藏  举报