(一)SpringMVC配置

第一步:修改pom.xml文件,添加依赖

<!-- SpringMVC依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>

第二步:修改web.xml文件,添加DispatcherServlet处理器

<!-- springMVC核心配置 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--spingMVC的配置路径 -->
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 拦截设置 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

第三步:新建spring-mvc.xml文件,添加相关配置

其中,spring-mvc.xml文件路径:src\main\resources

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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/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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!-- 扫描controller(controller层注入)
当配置了它后,Spring会自动的到com.mjl.controller
下扫描带有@controller @service @component等注解等类,将他们自动实例化 -->
<context:component-scan base-package="com.th.*"/>

<!-- 注册HandlerMapper、HandlerAdapter两个映射类,开启注解 -->
<mvc:annotation-driven />

<!-- 静态资源访问(不拦截此目录下的东西的访问) -->
<mvc:resources location="/jslib/img/" mapping="/jslib/img/**" />
<mvc:resources location="/jslib/jquery/" mapping="/jslib/jquery/**" />

<!-- ViewResolver视图解析器,对模型视图添加前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>

</beans>

 第四步:测试

(1)新建DemoController类,并新建2个测试方法

(2)新建demoMVC.jsp 和 stu.jsp文件,放在 /WEB-INF/view/ 路径下

(3)运行项目

输入地址:http://localhost:8080/tss/th/hello  转到demoMVC.jsp页面

输入地址:http://localhost:8080/tss/th/student  转到stu.jsp页面

 

项目代码:https://github.com/tianhu9102/tss

 

posted @ 2018-11-27 14:40  Vtianhu  阅读(199)  评论(2编辑  收藏  举报