典型配置可以有xml配置方法和java class配置方法,在Spring实战中作者建议用java class方法,原因是xml的错误检查机制没有java class方式好。在这里把两种配置方法都列在下面:
1.1.1 pom.xml
主要是 Maven 的配置文件,用来添加项目中依赖的jar包,包括spring-context/spring-web/spring-webmvc/commons-logging
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.mycompanygroupId>
<artifactId>SpringMvc-1artifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>SpringMvc-1name>
<properties>
<endorsed.dir>${project.build.directory}/endorsedendorsed.dir>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.3.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>4.3.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.0.RELEASEversion>
dependency>
<dependency>
<groupId>javaxgroupId>
<artifactId>javaee-web-apiartifactId>
<version>7.0version>
<scope>providedscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.1version>
<configuration>
<source>1.7source>
<target>1.7target>
<compilerArguments>
<endorseddirs>${endorsed.dir}endorseddirs>
compilerArguments>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-war-pluginartifactId>
<version>2.3version>
<configuration>
<failOnMissingWebXml>falsefailOnMissingWebXml>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-dependency-pluginartifactId>
<version>2.6version>
<executions>
<execution>
<phase>validatephase>
<goals>
<goal>copygoal>
goals>
<configuration>
<outputDirectory>${endorsed.dir}outputDirectory>
<silent>truesilent>
<artifactItems>
<artifactItem>
<groupId>javaxgroupId>
<artifactId>javaee-endorsed-apiartifactId>
<version>7.0version>
<type>jartype>
artifactItem>
artifactItems>
configuration>
execution>
executions>
plugin>
plugins>
build>
project>
1.1.2 WEB-INF 文件夹
该文件夹存在的意义在于:此文件夹客户端不可直接访问,只能通过controller中返回的String由服务器间接访问,从而保证了改文件夹的安全性。
1.1.2.1 jsp 文件夹
在配置dispatcher-servlet.xml中的viewResolver的时候前缀设置为 /WEB-INF/jsp/,后缀设置为 .jsp,这样controller中返回
String会由dispatcher servlet自动添加前后缀在webapp中索引view页面渲染模型。
1.1.2.2 web.xml
webapp标准配置文件,在该文件中配置dispatcher servlet(也就是spring mvc的servlet)以及其他服务器配置,如:session有效期,application context的文件配置等。
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>/WEB-INF/applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<welcome-file-list>
<welcome-file>redirect.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>dispatcherservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<load-on-startup>2load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>dispatcherservlet-name>
<url-pattern>*.dourl-pattern>
<url-pattern>*.jspurl-pattern>
servlet-mapping>
<session-config>
<session-timeout>
30
session-timeout>
session-config>
web-app>
1.1.2.3 dispatcher-servlet.xml
dispatcher-servlet中主要要配置扫描包,viewResolver,也可以配置url映射,但是在spring mvc4中主要是在java文件中通过@RequestMapping来进行映射的,所以在下面的xml样例中虽然有一个配置mapping的indexController但是不常用,建议用@RequestMapping来完成映射。
这里要注意:在web.xml中定义的dispatcher-servlet的name默认服务器会在同目录中搜寻【dispatcher-name】-servlet.xml的配置文件。所以当你不想费力自定义一个位置的时候就要注意dispatcher-servlet的命名方式了。
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<context:component-scan base-package="com.mycompany.springmvc.controller" />
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="login.jsp">indexControllerprop>
props>
property>
bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="login" />
beans>