SpringMVC环境搭建(二)

一、基于XML

1. 创建Maven Project,选择war,修改pom.xml

SpringMVC是依赖于Spring的,需要导入核心包。

<properties>
    <!-- JDK版本 -->
    <java.version>1.8</java.version>
    <!-- spring版本 -->
    <spring.version>4.1.6.RELEASE</spring.version>
</properties>

<dependencies>
    <!-- web project基本配置 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- 核心容器 之 spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- 核心容器 之 spring-beans -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- 核心容器 之 spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- 核心容器 之 spring-context-support -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- 核心容器 之 spring-expression -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <!-- spring web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- spring web mvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
</dependencies>

2. 在src/main/webapp下创建META-INF、WEB-INF,在WEB-INF下创建web.xml

在web.xml中配置前端控制器:

<!-- 配置前端控制器DispatcherServlet -->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <!-- mvc配置文件的文件名以及目录 -->
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

3. 在src/main/resources下创建springmvc.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/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <!-- 1. 配置处理器映射器 将bean的name作为url进行查找,需要在配置Handler时指定bean name(就是url) -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
    <!-- 2. 配置处理器适配器,所有适配器都得实现 HandlerAdapter接口 -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
    <!-- 3. 配置Handler -->
    <bean name="/hello.do" class="com.linhw.demo.mvc.HelloController" />

    <!--配置视图解析器  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 返回视图页面的前缀 不能写成 /WEB-INF/jsp,最后的"/"一定要写-->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <!-- 返回页面的后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>
    
</beans>

4. 创建HelloController

public class HelloController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView modelView = new ModelAndView();
        modelView.setViewName("index");
        return modelView;
    }
}

5. 在WEB-INF/jsp目录下创建index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello world!
</body>
</html>

6. 浏览器访问

访问地址:http://ip:port/项目名/hello.do

二、基于注解

上面是基于 XML 的配置,我们发现每个 Handler(Controller) 类只能有一个方法,在实际开发中肯定是不可能这样来进行开发的。接下来我们就讲解实际开发中用的最多的基于注解配置的SpringMVC配置。

1. 修改springmvc.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/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 
        1. 注解处理器映射器:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
        2. 注解处理器适配器:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
     -->
    <!-- 
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> 
    -->

    <!--使用mvc:annotation-driven可以代替上面的映射器和适配器 这里面会默认加载很多参数绑定方法,比如json转换解析器就默认加载,所以优先使用下面的配置 -->
    <mvc:annotation-driven/>

    
    <!--单个配置Handler,不使用注解  -->
    <!-- <bean class="com.linhw.demo.mvc.HelloController"></bean> -->
    <!--批量配置Handler,指定扫描的包全称  -->
    <context:component-scan base-package="com.linhw.demo.mvc.controller"/>
    
    <!--配置视图解析器  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 返回视图页面的前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <!-- 返回页面的后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>
    
</beans>

2. 修改Controller

@Controller
public class HelloController2 {

    @RequestMapping("/hello")
    public ModelAndView showPage(){
        ModelAndView modelView = new ModelAndView();
        modelView.setViewName("index");
        return modelView;
    }
}

 

posted @ 2019-09-22 10:22  codedot  阅读(648)  评论(0编辑  收藏  举报