【maven系列】:maven构建模块化项目之SpringMVC (02)
一、准备工作
(1)准备web项目所需要的jar包,maven web项目pom.xml
<!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- web项目需要的jar --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp.version}</version> <scope>provided</scope> </dependency> <!-- web项目需要的jar END --> <!-- SpringWeb包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring-version}</version> </dependency> <!-- SpringMVC包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency>
parent 项目的pom.xml文件 管理每个jar包的版本
<!-- jar包管理 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.11</junit.version> <mysql.version>5.1.34</mysql.version> <servlet.version>3.1.0</servlet.version> <jstl.version>1.2</jstl.version> <jsp.version>2.2.1-b03</jsp.version> <log4j.version>1.2.17</log4j.version> <spring-version>4.1.6.RELEASE</spring-version> </properties>
二、项目分包并配置Spring配置文件
(1)serviceweb分好包后开始配置Spring
(2)在这里博主比较喜欢将Spring的xml文件分为多个如:spring.xml (负责配置文件的引入、上下文扫描)、spring-mvc.xml(负责SpringMvc的一些配置)、spring-hibernate.xml(或者其他ORM的配置信息)PS:个人觉得将所有配置信息都放在 applicationContext.xml中会有些囊肿,看起来也很费劲不明了。
spring.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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<!-- 自动扫描dao和service包(自动注入) --> <context:component-scan base-package="com.nc.rms.service" /> <context:component-scan base-package="com.nc.rms.dao" /> </beans>
spring-mvc.xml配置信息:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.nc.rms.manage.web.*" /> <!-- 开启mvc注解 --> <mvc:annotation-driven /> <!-- 设置jsp访问前缀, <property name="prefix" value="/WEB-INF/view/"></property>后台就不需要在写这段 直接写下面的jsp页面名称,不需要在加上/WEB-INF/view/ --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <!-- 设置文件后缀名 <property name="suffix" value=".jsp"></property> --> <property name="suffix" value=".jsp"></property> </bean> </beans>
(3)想知道一个项目中所运用到哪些框架首先就得想到 web.xml 这个神奇的xml~废话不多少直接看码说话:
<?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" 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" version="2.5"> <display-name>Rights Management System</display-name> <!-- 过滤字符集 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 监听servletContext,启动contextConfigLocation中的spring配置信息,注:加载Spring配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 设置Spring配置文件所在路径如果有多个逗号隔开 如:classpath:spring.xml,classpath:spring-hibernate.xml--> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring.xml </param-value> </context-param> <!-- SpringMVC--> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- 此处配置的是SpringMVC的配置文件 --> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- SpringMVC制定访问方式 如:*.do *.action / ,不建议/--> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- 初始化监听 --> <listener> <listener-class>com.nc.rms.manage.web.listener.InitListener</listener-class> </listener> </web-app>
这里代码的颜色有点不对,看官们直接可以忽略~绿色的可不是注掉的啊。。
(4)接下来就去写个Controller看看效果如何:
package com.nc.rms.manage.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class UserController { @RequestMapping("/toUserHomePage") public ModelAndView toUserHomePage(){ System.out.println("SpringMVC"); return new ModelAndView("Helloworld"); } }
(5)运行下tomcat看看成功与否~