美文网首页
JavaWeb SpringMVC

JavaWeb SpringMVC

作者: dawsonenjoy | 来源:发表于2019-04-26 10:43 被阅读0次

SpringMVC主要起到了Servlet的作用,用于拦截和处理请求

所需Jar包

主要包括spring框架所需的jar包(spring-aopspring-beansspring-contextspring-corespring-expressioncommons-logging)以及spring-webmvc.jar

使用步骤

1.配置web.xml

在web.xml文件当中进行如下配置后,将会拦截所有请求,并交给SpringMVC来处理:

<servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
        <!-- springMVC配置文件的位置 -->
    </init-param>
    <load-on-startup>1</load-on-startup>
    <!-- 启动时自动生效 -->
</servlet>

<servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
    <!-- /代表拦截一切请求 -->
</servlet-mapping>

注:
SpringMVC对于拦截请求的路径配置和Servlet的不太相同(如:在Servlet中/*是匹配所有路径,而SpringMVC中是/*),下面列举几个常见的路径配置:

/aaa  拦截/aaa开头的请求
/aaa/bbb.do  拦截该指定请求
.action  拦截.action结尾的请求
2.编写请求处理类
@Controller
// 说明是个控制器
public class Test {
    @RequestMapping("xxx")
    // 配置访问路径
    public String test1(){
        return "aaa";
    }
}
3.配置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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        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-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    
    <context:component-scan base-package="com.test"></context:component-scan>
    <!-- 扫描器 -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!-- 对所有springmvc拦截请求路径进行配置 -->
        <property name="prefix" value="/views/"></property>
        <!-- 配置前缀路径 -->
        <property name="suffix" value=".jsp"></property>
        <!-- 配置后缀路径 -->
    </bean>
</beans>

由于对mvc请求配置了前缀和后缀,所以对于前面的访问路径项目/xxx,那么结果就会访问到views路径下的aaa.jsp文件,即相当于访问:项目/views/aaa.jsp

注解配置映射

从上面可看出SpringMVC使用@RequestMapping()来配置访问的路径,并且是从外到内依次叠加路径,举例:

@Controller
@RequestMapping("xxx")
public class Test {
    @RequestMapping(value="yyy/zzz", method=RequestMethod.GET, params={"name","id=1","age!=1","!pwd"})
    public String test1(){
        return "aaa";
        // get方法访问:项目/xxx/yyy/zzz,
        // 并且必须传入name参数、id参数值为1、age参数值不为1、不含pwd参数
        // 才会访问到该方法,返回时默认使用请求转发的方式法
    }
    @RequestMapping(value="aaa", headers={"user-agent=..."})
    public String test2(){
        return "aaa";
        // 请求头的user-agent为...才能访问
        // 多个参数用逗号隔开
    }
    @RequestMapping(value="bbb/c?c/test")
    public String test3(){
        return "aaa";
        // bbb/c单个字符c/test才能访问
        // 如:bbb/ccc/test
    }
    @RequestMapping(value="bbb/*/test")
    public String test4(){
        return "aaa";
        // bbb/任意个字符(0或多个)/test才能访问
        // 如:bbb/sdadsa/test
    }
    @RequestMapping(value="bbb/**/test")
    public String test5(){
        return "aaa";
        // bbb/任意目录/test才能访问
        // 如:bbb/ccc/ddd/test
    }
}

获取访问路径值

如果要获取访问路径上的值并传入对应的执行方法当中,可以通过配置路径{值}获取,并通过注解@PathVariable传入对应方法当中,举例:

public class Test {
    @RequestMapping(value="ccc/{name}")
    public String test(@PathVariable("name") String name){
        System.out.println(name);
        return "aaa";
        // 获取name,比如访问ccc/abc
        // 那么就会输出abc
    }
}

相关文章

网友评论

      本文标题:JavaWeb SpringMVC

      本文链接:https://www.haomeiwen.com/subject/oqbtnqtx.html