SpringMVC初始

原有方式

Controller控制层

package controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * 这是一个控制层的类
 * 这个类最早期用Tomcat管理的时候
 * 是有一些规则的
 * 1.继承                     实现Controller
 * 2.重写                     重写handleRequest
 * 3.参数 request response    两个参数
 * 4.返回值                   返回值  ModelAndView
 * 5.异常等                   异常
 *  请求与真实资源的对应关系-----配置文件SpringMVC自己的文件
 */
public class TestController implements Controller {

    @Override
    public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
        return null;
    }
}

ApplicationContext.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/c"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--配置一个请求和真实资源(Controller-Bean)的关系- 可以用注解@Controller代替-->
    <bean id="testController" class="controller.TestController"></bean>
    <!--请求和这个类对象之间的映射关系 通过请求找到他,可以用注解@RequestMapping代替-->
    <!--DispatcherServlet类有自己的小弟,负责处理映射关系 SimpleUrlHandlerMapping-->
    <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <!--真实的请求和类对应关系
                   key:请求  testController:controller类对象的id值-->
                <prop key="toindex.do">testController</prop>
            </props>
        </property>
    </bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
         version="4.0">
    <!--配置SpringMVC核心入喉执行类-->
    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--
            如果ApplicationContext.xml的文件名是自己起的,并且在src下,就要添加init-param
            param-name的值必须为contextConfigLocation
            如果ApplicationContext.xml的文件名是mvc-servlet.xml,并在WEB-INF文件中,则不写init-param
        -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:ApplicationContext.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

注解的方式

Controller控制层

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 注解的方式管理
 * @Controller为了代替原有xml中的bean标签,
 * 只是告知Spring容器,有一个Controller类对象需要交给注解管理,放在Spring底层的map容器中
 * @RequestMapping为了说明请求的名字与真实类名对应的关系
 */
@Controller
@RequestMapping("toindex.do")//请求名与类名的对应关系
public class AnnotationController {

}

ApplicationContext.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/c"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <!--注解形式-->
    <!--将bean标签去掉 改为扫面Controller注解的包-->
    <context:component-scan base-package="controller"></context:component-scan>
    <!--将请求与类名的对应关系用注解代替,就要添加一个新的配置
    开启注解驱动,让@RequestMapping起作用
    相当于是加载了HandlerMapping类对象-->
    <mvc:annotation-driven></mvc:annotation-driven>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
         version="4.0">
    <!--配置SpringMVC核心入喉执行类-->
    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--
            如果ApplicationContext.xml的文件名是自己起的,并且在src下,就要添加init-param
            param-name的值必须为contextConfigLocation
            如果ApplicationContext.xml的文件名是mvc-servlet.xml,并在WEB-INF文件中,则不写init-param
        -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:ApplicationContext.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值