最近在看spring in action第3版,这里搭建一个简单的spring mvc,也算书没白看,当然老鸟可以不屑了,这里只是给自己做个笔记,配置也尽量删烦就简,

Spring MVC的核心是DispatcherServlet,这个Servlet充当前端控制器。与其他Servlet一样,必须在web应用程序的文件中进行配置。

1. 从web.xml开始

1.web.xml:

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5"

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_2_5.xsd">

<!-- contextConfigLocation参数指定一个spring配置文件路径列表,路径是相对于应用程序根目录的。 -->

<!-- 需要告诉ContextLoaderListener需要加载哪些配置文件。 如果没有指定,会依照约定上下文加载器会查找/WEB-INF/applicationContext.xml这个Spring配置文件。-->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

classpath:xml/springContext.xml

</param-value>

</context-param>

<!-- ContextLoaderListener是一个Servlet监听器,除了DispatcherServlet创建的应用上下文以外,它能够加载其他的配置文件到spring应用上下文中。  -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- DispatcherServlet会根据一个XML文件来加载其spring应用上下文,而这个文件的名字基于它的<servlet-name>属性来确定,这里为springmvc-servlet -->

<!-- 关于<load-on-startup>解释:

转自xzc.Log,原文链接http://www.blogjava.net/xzclog/archive/2011/09/29/359789.html

贴一段英文原汁原味的解释如下:

Servlet specification:

The load-on-startup element indicates that this servlet should be loaded

(instantiated and have its init() called) on the startup of the web application.

The optional contents of these element must be an integer indicating the order in which

the servlet should be loaded. If the value is a negative integer, or the element is not present,

the container is free to load the servlet whenever it chooses.   If the value is a positive integer

or 0, the container must load and initialize the servlet as the application is deployed.

The container must guarantee that servlets marked with lower integers are loaded before servlets

marked with higher integers. The container may choose the order of loading of servlets

with the same load-on-start-up value.

翻译过来的意思大致如下:

1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。

2)它的值必须是一个整数,表示servlet应该被载入的顺序

2)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet;

3)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。

4)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。

5)当值相同时,容器就会自己选择顺序来加载。

所以,<load-on-startup>x</load-on-startup>,中x的取值1,2,3,4,5代表的是优先级,而非启动延迟时间。 -->

<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:xml/springmvc-servlet.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>

</web-app>

2.springmvc-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:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- spring自动检测组件,即不在xml文件中配置bean,而给我们的类加上spring组件注解。但是注解注入会失去一定的灵活性 -->

<!-- 定义扫描路径,不使用默认的扫描方式 -->

<!-- @Component是所有受Spring管理组件的通用形式;而@Repository、@Service和 @Controller则是@Component的细化,分别对应了持久化层、服务层和表现层)。 -->

<!-- 子标签进行过滤,use-dafault-filters=”false”的情况下:<context:exclude-filter>指定的不扫描,<context:include-filter>指定的扫描  -->

<context:component-scan base-package="com.aries.mo" use-default-filters="false">

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /><!-- 只扫面@Controller注解的类 -->

</context:component-scan>

<!-- 默认的注解映射的支持,告知Spring,我们启用注解驱动。 -->

<!-- 这个标签注册了Spring MVC分发请求到控制器所必须的DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter实例 -->

<!-- 第一个是HandlerMapping的实现类,它会处理@RequestMapping 注解,并将其注册到请求映射表中。 -->

<!-- 第二个是HandlerAdapter的实现类,它是处理请求的适配器,说白了,就是确定调用哪个类的哪个方法,并且构造方法参数,返回值。 -->

<mvc:annotation-driven conversion-service="conversionService"/>

<!-- <mvc:resources>建立一个服务于静态资源的处理器。

属性mapping设置为/resource/**,表示路径必须以/resource开始,而且包括它的任意子路径。

属性location表示要提供服务的文件位置。配置表示,

所有以/resource开头的请求都会自动由应用程序根目录下的/resource目录提供服务。

因此,我们所有的图片、样式表、javascript以及其他静态资源都必须房子应用的/resource目录下 -->

<mvc:resources mapping="/resource/**" location="/resource/" />

<!-- 视图解析器 -->

<!-- DispatcherServlet会查找一个视图解析器来将控制器返回的逻辑视图名称转换成渲染结果的实际视图,

视图解析器的工作是将逻辑视图的名字与org.springframework.web.servlet.view的实现相匹配,可以认为,

视图解析器所做的就是将视图名称与JSP进行匹配。spring提供多种视图解析器选择,这里选择InternalResourceViewResolver -->

<!-- 依照约定优于配置,InternalResourceViewResolver将逻辑视图解析为View对象,而该对象将渲染的任务委托给Web应用程序

上下文中的一个模板(通常JSP),它通过为逻辑视图名称添加前缀和后缀来确定Web应用程序中模板的路径。 -->

<!-- 假设应用的所有JSP放在"/WEB-INF/views/"目录下,即有如下的配置 -->

<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/views/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

</beans>

3.springContext.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- 扫描注解实体bean -->

<context:component-scan base-package="com.aries.mo" use-default-filters="false">

<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />

<context:include-filter type="annotation" expression="org.springframework.stereotype.Component" />

<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />

</context:component-scan>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close">

<property name="driverClass" value="com.mysql.jdbc.Driver" />

<property name="jdbcUrl" value="jdbc:mysql:///db_aries" />

<property name="user" value="aries" />

<property name="password" value="1991" />

<property name="minPoolSize" value="5" />

<property name="maxPoolSize" value="15" />

<property name="initialPoolSize" value="5" />

<property name="checkoutTimeout" value="3000" />

<property name="idleConnectionTestPeriod" value="180" />

<property name="maxIdleTime" value="180" />

<property name="propertyCycle" value="300" />

<property name="numHelperThreads" value="10" />

<property name="maxStatements" value="100" />

<property name="maxStatementsPerConnection" value="10" />

<property name="acquireIncrement" value="3" />

<property name="acquireRetryAttempts" value="30" />

<property name="breakAfterAcquireFailure" value="false" />

</bean>

<!-- spring与mybatis整合 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="configLocation" value="xml/mybatisConfig.xml" />

</bean>

</beans>

4.mybatisConfig.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<settings>

<setting name="cacheEnabled" value="false" />

</settings>

<typeAliases>

<typeAlias alias="User" type="com.aries.mo.entity.User"/>

</typeAliases>

<mappers>

<mapper resource="xml/mybatis/UserDao.xml"/>

</mappers>

</configuration>

5.UserDao.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- 通过命名空间和对应id检索对应的sql语句 -->

<mappernamespace="userDao">

<select id="getUesr" resultType="com.aries.mo.entity.User">

select * from test

</select>

</mapper>

项目结构简单如图,

简单测试代码;

UserController.java

package com.aries.mo.Controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

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

import org.springframework.web.bind.annotation.RequestParam;

import com.aries.mo.service.UserService;

@Controller

@RequestMapping(value="/user")

public class UserController {

@Autowired

private UserService userService;

@RequestMapping(value="/text")

public String TestUser(Model model/*,@RequestParam("userName") String userName*/) {

model.addAttribute("userName", userService.getName());

return "userShow";

}

}

UserDao.java

package com.aries.mo.dao;

import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import org.springframework.stereotype.Repository;

import com.aries.mo.entity.User;

@Repository

public class UserDao extends SqlSessionDaoSupport {

@SuppressWarnings("unchecked")

public List<User> getUesr(){

return getSqlSession().selectList("userDao.getUesr");

}

}

 

第7章 使用springMVC构建Web应用程序 7.1 springMVC配置的更多相关文章

  1. redis实战笔记(2)-第2章 使用 Redis构建Web应用

    第2章 使用 Redis构建Web应用 本章主要内容   1.登录cookie 2.购物车cookie 3.缓存生成的网页 4.缓存数据库行 5.分析网页访问记录   本章的所有内容都是围绕着发现并解 ...

  2. Node.js高级编程读书笔记 - 4 构建Web应用程序

    Outline 5 构建Web应用程序 5.1 构建和使用HTTP中间件 5.2 用Express.js创建Web应用程序 5.3 使用Socket.IO创建通用的实时Web应用程序 5 构建Web应 ...

  3. 使用ASP.NET 构建 Web 应用程序快速入门-8小时的免费培训视频

    - Scott Hanselman的中文博客[转载] [原文发表地址] Building Web Apps with ASP.NET Jump Start - 8 Hours of FREE Trai ...

  4. maven构建web项目,用jetty测试的配置pom.xml

    maven构建web项目,用jetty测试的配置pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...

  5. spring mvc构建WEB应用程序入门例子

    在使用spring mvc 构建web应用程序之前,需要了解spring mvc 的请求过程是怎样的,然后记录下如何搭建一个超简单的spring mvc例子. 1) spring mvc的请求经历 请 ...

  6. Web 应用程序项目 MvcApplication1 已配置为使用 IIS。

    今天网上下了一个项目,加载不了,并报如下错误: Web 应用程序项目 MvcApplication1 已配置为使用 IIS. 若要访问本地 IIS 网站,必须在管理员帐户的上下文中运行 Visual ...

  7. Web 应用程序项目 XXXX 已配置为使用 IIS。 无法访问 IIS 元数据库。您没有足够的特权访问计算机上的 IIS 网站。(转载)

    Web 应用程序项目 XXXX 已配置为使用 IIS. 无法访问 IIS 元数据库.您没有足够的特权访问计算机上的 IIS 网站. 2012年05月19日 ⁄ 综合 ⁄ 共 261字 ⁄ 字号 小 中 ...

  8. Web应用程序项目XXXX已配置为使用IIS。无法访问IIS 元数据库。您没有足够的特权访问计算机上的IIS

    错误图片:

  9. ADF_Starting系列9_使用EJB/JPA/JSF通过ADF构建Web应用程序之测试J2EE Container

    2013-05-01 Created By BaoXinjian

随机推荐

  1. Angular JS 中 ng-controller 值复制和引用复制

    我们知道在使用ng-app或者ng-controller指令的时候,都会创建一个新的作用域($rootScope或者是$scope),并且在使用ng-controller指令创建的作用域会继承父级作用 ...

  2. 游戏开发3D基础知识

    概念学习: 向量 向量简介 我们将所有彼此平行的向量进行平移,使其起点与坐标原点重合,当某一向量的起始端与坐标原点重合,我们成该向量处于标准位置.这样,我们就可用向量的终点坐标来描述一个处于标准位置的 ...

  3. 林大妈的JavaScript基础知识(三):JavaScript编程(4)数组

    数组,是一段线性分配的,具有非常高性能的数据结构.简单地说,数组以连续的空间存储,通过整数地计算偏移量访问其中的元素,将读取修改的时间复杂度降低至O(1),我们称之为猝发式存取.是不是非常期待?没错, ...

  4. .c和.h文件的区别

    .h文件(头文件): 一般写一些函数声明.宏定义.结构体等内容. 其实就是将各个.c文件中重复的声明.宏定义.结构体,枚举变量等提取出来,放进一个新的文件中,便于其他.c文件共享这部分的代码,同时也方 ...

  5. C#打开并选择特定类型文件并返回文件名

    public string[] GetOpenFileDialogReturnFileFullName(bool multiSelect = false)         {             ...

  6. MySQL操作命令梳理(2)

    一.表操作 在mysql运维操作中会经常使用到alter这个修改表的命令,alter tables允许修改一个现有表的结构,比如增加或删除列.创造或消去索引.改变现有列的类型.或重新命名列或表本身,也 ...

  7. Java:控制反转(IoC)与依赖注入(DI)

    很长一段时间里,我对控制反转和依赖注入这两个概念很模糊,闭上眼睛想一想,总有一种眩晕的感觉.但为了成为一名优秀的 Java 工程师,我花了一周的时间,彻底把它们搞清楚了. 01.紧耦合 在我们编码的过 ...

  8. template.demo.js

    <!DOCTYPE html><html><head> <title>index</title> <meta charset=&quo ...

  9. JavaWeb——Filter过滤器

    1.Filter的目的 Filter用于在Servlet之前检测和修改请求和响应,它可以拒绝.重定向或转发请求.常见的有这几种: 日志过滤器 使用过滤器记录请求,提供请求日志记录,还可以添加追踪信息用 ...

  10. sql server数据库查询链接服务器

    服务器对象->链接服务器: 或者 select  * from sys.servers: 找到服务器对象名称 select  * from [服务器对象名称].[数据库名称].dbo.[表名]: