SpringMvc环境搭建

springmvc 执行原理 
页面发送请求,控制器根据请求找到相应的映射(方法),执行功能后,根据返回结果,经过视图解析器处理,跳转到相应页面,呈现处理结果。

环境搭建步骤 
1、导入相关 jar 包 
百度云自取 搭建springmvc需要的jar包

2、在 web.xml 中配置 dispatchServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- 配置 DispatcherServlet -->
    <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>          
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

3、编写 springmvc.xml 文件,配置 视图解析器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" 
    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-4.0.xsd 
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd ">

    <!-- 自动扫描 -->
    <context:component-scan base-package="com.xiao.leave"></context:component-scan>

    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/view/"></property>
        <property name="suffix" value=".jsp"/>
    </bean>


</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

4、编写控制器和相关页面

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.xiao.leave.entities.Students;

@SessionAttributes(value={"stu"},types={String.class})
@Controller
@RequestMapping("/login")
public class LonginAction {
    private static final String SUCCESS = "success";



    @RequestMapping("/testSessionAttribute")
    public String testSessionAttribute(Map<String, Object> map){
        Students stu = new Students("xiao", 24, "123456");
        map.put("stu", stu);
        map.put("addr", "北京");

        return SUCCESS;
    }

    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        String viewName = SUCCESS;
        ModelAndView modelandview = new ModelAndView(viewName);
        modelandview.addObject("time",new Date());

        return modelandview;
    }

    @RequestMapping("/testMap")
    public String testMap(Map<String, Object> map){
        map.put("names", Arrays.asList("tom", "mike", "lily"));
        System.out.println(map.getClass().getName());

        return SUCCESS;
    }

    @RequestMapping("/testPojo")
    public String testPojo(Students stu){
        System.out.println("Student:" + stu);

        return SUCCESS;
    }

    @RequestMapping("testRequestParam")
    public String testRequestParam(@RequestParam(value="username") String un, 
                                    @RequestParam(value="age",required=false,defaultValue="0") int age){
        /**
         * RequestParam 映射请求参数
         * value:请求参数的参数名
         */
        System.out.println("testRequestParam: username = " + un + ", age = " + age);

        return SUCCESS;
    }

    @RequestMapping("/testPathVariable/{id}")
    public String testPathVariable(@PathVariable("id")Integer id){
        //PathVariable 可以映射 url 中占位符到目标方法的参数中
        System.out.println("testPathVariable  ==" + id);

        return SUCCESS;
    }

    @RequestMapping("testAntPath/*/aa")
    public String testAntPath(){
        // * 匹配任意个字符,? 匹配一个字符,** 匹配多层路径

        System.out.println("testAntPath");

        return SUCCESS;
    }

    @RequestMapping(value="/testparaAndMethod", params={"username","age!=10"}, method=RequestMethod.POST)
    public String testparaAndMethod(){
        /**
         * value表示请求路径的映射
         * params表示传入的参数;!=表示有该参数,但是不等于某值,
         * method表示请求方式
         */

        System.out.println("testparaAndMethod");

        return SUCCESS;
    }

    @RequestMapping("/helloworld")
    public String helloworld(){
        System.out.println("helloworld");

        return SUCCESS;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106

success.jsp

<body>
    <h2>恭喜!登录成功</h2>

    <h4>names: ${requestScope.names} </h4>

    <h4>time: ${requestScope.time} </h4>

    <h4>request stu: ${requestScope.stu} </h4>

    <h4>session stu: ${sessionScope.stu} </h4>

    <h4>request addr: ${requestScope.addr} </h4>

    <h4>session addr: ${sessionScope.addr} </h4>


  </body>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

login.jsp


  <body>

    <a href="login/testSessionAttribute">testSessionAttribute</a><br/>

    <a href="login/testModelAndView">testModelAndView</a><br/>

    <a href="login/testMap">testMap</a><br/>

    <form action="login/testPojo">
        姓名:<input type="text" name="sname"><br/>
        年龄:<input type="text" name="sage"><br/>
        手机:<input type="text" name="stel"><br/>
        省份:<input type="text" name="saddr.province"><br/>
        城市:<input type="text" name="saddr.city"><br/>

        <input type="submit" value="提交">
    </form>

    <a href="login/testRequestParam?username=xiao&age=24">testRequestParam</a><br>

    <a href="login/testPathVariable/1">testPathVariable</a><br/>

    <a href="login/testAntPath/hello/aa">testAntPath</a><br/>

    <form action="login/testparaAndMethod?username=xiao&age=11" method="post">
        <input type="submit" value="登录"/>
    </form>

    <a href="login/helloworld">hello world</a><br/>
  </body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

点击 login.jsp 中的链接,能成功跳转到 success.jsp 页面,则说明环境搭建成功!(testPojo 需要写一个 pojo,加入get set toString 方法)


版权声明:本文为qq_40285302原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。