• SpringMVC (六)注解式开发


    之前我们写springmvc的案例的时候所有的操作都是用配置文件配置的,现在我们来说另一种写案例的方式,使用注解式开发,在后面的开发中我们也都是这种方式进行开发

    首先我先用注解式开发写springmvc的第一个案例

    编写一个类:AnnotationController

    @Controller
    public class AnnotationController {
    @RequestMapping(value = "/first")
        public String doFirst(){
            return "first";
        }
    }

    然后在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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:content="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        <!--包扫描器-->
        <content:component-scan base-package="demo07Annotation"></content:component-scan>
    
    </beans>

    不要忘记修改web.xml的配置文件的路径

    这种方式的配置,在配置文件的少了很多

    访问的时候直接访问@RequestMapping(value = "first")的value的值就可以了

    下面在来看看通配符的用法

    二。通配符的使用

     //通配符的使用
        @RequestMapping("/second*")//*代表0或者多个字符,只要以second开头就可以
        public String doSecond(){
            return "first";
        }
        @RequestMapping("/*six")//*代表0或者多个字符,只要以six结尾就可以
        public String doSix(){
            return "first";
        }
    
        @RequestMapping("/**/third")//以third结尾,在之前可以有0到多级目录
        public String doThird(){
            return "first";
        }
    
        @RequestMapping("/*/four")//在four之前必须而且只能有一级目录
        public String doFour(){
            return "first";
        }

    三。请求方式限定

    就是你访问的时候用什么方式访问的是get还是post,它会限制你的访问方式

    package demo07Annotation;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Created by mycom on 2018/3/18.
     */
    @Controller
    @RequestMapping("/logger")
    public class AnnotationController {
        @RequestMapping(value = "/*first",method = RequestMethod.GET)//只能以get方式访问
        public String doFirst(){
            return "first";
        }
        @RequestMapping(value = "/*five",method = RequestMethod.POST)//只能以post方式访问
        public String doFive(String uname,String upwd){
            System.out.println(uname);
            System.out.println(upwd);
            return "first";
        }
        //通配符的使用
        @RequestMapping("/second*")//*代表0或者多个字符,只要以second开头就可以
        public String doSecond(){
            return "first";
        }
        @RequestMapping("/*six")//*代表0或者多个字符,只要以six结尾就可以
        public String doSix(){
            return "first";
        }
    
        @RequestMapping("/**/third")//以third结尾,在之前可以有0到多级目录
        public String doThird(){
            return "first";
        }
    
        @RequestMapping("/*/four")//在four之前必须而且只能有一级目录
        public String doFour(){
            return "first";
        }
    
    }
  • 相关阅读:
    建设工程造价数据服务云平台(计价BIM)
    工程造价数据服务云平台(造价BIM)
    工程造价数据服务云平台(造价BIM)
    ie9下对象for..in..的bug
    JavaScript闭包
    JavaScript如何比较两个对象字面量是否一样(key 和 value都对比)
    JavaScript转码和编码
    JavaScript中正则编码
    Sizzle 官方API翻译
    JSONP跨域
  • 原文地址:https://www.cnblogs.com/my-123/p/8644463.html
Copyright © 2020-2023  润新知