• spring mvc 之初体验


    Spring MVC最简单的配置

      配置一个Spring MVC只需要三步:

    1. 在web.xml中配置Servlet;
    2. 创建Spring MVC的xml配置文件;
    3. 创建Controller和View
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     5          version="3.1">
     6 
     7     <!-- spring mvc配置开始  -->
     8     <servlet>
     9         <servlet-name>test</servlet-name>
    10         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    11         <load-on-startup>1</load-on-startup>
    12     </servlet>
    13 
    14     <servlet-mapping>
    15         <servlet-name>test</servlet-name>
    16         <url-pattern>/</url-pattern>
    17     </servlet-mapping>
    18     <!-- spring mvc配置结束  -->
    19 
    20     <welcome-file-list>
    21         <welcome-file>index</welcome-file>
    22     </welcome-file-list>
    23 </web-app>

    所配置的Servlet是DispatcherServlet类型,它就是Spring MVC的入口,Spring MVC的本质就是一个Servlet。在配置DispatcherServlet的时候可以设置contextConfigLocation参数来指定Spring MVC配置文件的位置,如果不指定就默认使用WEB-INF/[ServletName]-servlet.xml文件。

    servlet.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
     4    xmlns:context="http://www.springframework.org/schema/context"
     5    xmlns:mvc="http://www.springframework.org/schema/mvc"
     6    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     7    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
     8    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
     9 
    10     <mvc:annotation-driven/>
    11     <context:component-scan base-package="com.excelib" />
    12 </beans>

    <mvc:annotation-driven/>是Spring MVC提供的一键式的配置方法,配置此标签后Spring MVC会自动做一些注册组件之类的事情。

    context:component-scan base-package="com.excelib" />扫描通过注释配置的类,还可以通过context:include-filter子标签来设置只扫描@Controller就可以了,别的交给Spring容器管理

    1 <context:component-scan base-package="com.excelib" use-default-filters="false">
    2 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    3 </context:component-scan>
     1 @Controller
     2 public class GoController {
     3     private final Log logger = LogFactory.getLog(GoController.class);
     4     //处理HEAD类型的”/”请求,HEAD类型的请求可以用来检测服务器的状态,因为它不返回body所以比GET请求更省网络资源
     5     @RequestMapping(value={"/"},method= {RequestMethod.HEAD})
     6     public String head() {
     7         return "go.jsp";
     8     }
     9     //处理GET类型的"/index"和”/”请求
    10     @RequestMapping(value={"/index","/"},method= {RequestMethod.GET})
    11     public String index(Model model) throws Exception {
    12         logger.info("======processed by index=======");
    13         //返回msg参数
    14         model.addAttribute("msg", "Go Go Go!");
    15         return "go.jsp";
    16     }
    17 }

    若果没有配置ViewResolver,Spring MVC将默认使用org.springframework.web.servlet.view.InternalResourceViewResolver作为ViewResolver。

  • 相关阅读:
    export,source
    zookeeper安装笔记
    centos修改启动顺序,登录后提示,启动级别,主机名,免密登录
    CentOS卸载系统自带的OpenJDK
    处理有外键约束的数据
    linux iptables
    centos httpd服务做yum本地源,以及安装Mysql
    Linux命令(一)grep查询
    LaTeX符号和图片
    LaTeX文章结构
  • 原文地址:https://www.cnblogs.com/kevin443/p/6771693.html
Copyright © 2020-2023  润新知