SpringMVC

环境搭建:

1.创建maven工程(选择web app)

2.添加pom依赖

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>

3.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

<context:component-scan base-package="org.lanqiao.action"/>

<mvc:annotation-driven/><!-- 可以通过注解 -->
<!-- 视图的类型 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>//表示触动时寻找的路径(/index.action)
<property name="suffix" value=".jsp"/>//路径下加载文件的类型----->在WEB-INF下建立jsp包,然后写。jsp文件
</bean>
</beans>

4.web.xml文件的配置

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<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:spring-mvc.xml</param-value>
</init-param>
<!-- 默认加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.action</url-pattern>//请求方法以点action结尾
</servlet-mapping>
</web-app>

5.在源码包下建立indexAction.java

@Controller
public class indexAction {
@RequestMapping(value="index")
public String index(){
System.out.println("接收到index.action的请求");
return "welcome";//即访问welcome.jsp,换成别的名称会报404(找不到资源,因为没有此jsp文件)
}
}

6.运行工程时 访问的是index.jsp

在此地址后加入index.action时访问的是welcome.jsp

文件目录结构

 

posted on 2017-09-01 16:48  青葙  阅读(118)  评论(0编辑  收藏  举报