SpringMvc环境搭建

一、步骤

1.创建maven-web项目

2.补全目录

3.添加依赖、加入tomcat插件

4.创建控制器类,跳转到index.jsp

5.新建Spring MVC框架配置文件springmvc.xml

6.编写web.xml

7.获取参数

二、实操

1.创建maven-web项目

SpringMvc环境搭建_第1张图片

2.补全目录

SpringMvc环境搭建_第2张图片

3.添加依赖、加入tomcat插件




  4.0.0

  com.zxs
  TestSpringMVC
  1.0-SNAPSHOT
  war

  
  
    
      org.springframework
      spring-webmvc
      5.3.16
    
  

  
    
      
      
        org.apache.tomcat.maven
        tomcat7-maven-plugin
        2.2
        
          /testspringmvc01
          8888
        
      
    
  


4.创建控制器类,跳转到index.jsp

package com.zxs.controller;

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

@Controller
public class TestController {

    @RequestMapping("test1")
    public String test1(){
        return "index.jsp";
    }

}

5.新建Spring MVC框架配置文件springmvc.xml




    
    
    
    


6.编写web.xml



  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      
      classpath:springmvc.xml
    
    
    
    1
  
  
    springmvc
    
    /
  

7.获取参数

7.1 获取普通参数        

SpringMvc环境搭建_第3张图片

@RequestMapping("testPara1")
    public String testPara1(String name ,int age){
        System.out.println(name+"------"+age);
        return "index.jsp";
    }

 localhost:8888/testspringmvc01/testPara1?name=jack&age=18

7.2 使用类对象作为控制单元参数

SpringMvc环境搭建_第4张图片

package com.zxs.pojo;


public class Person {
    private String name;
    private int age;
    private double score;

    public Person(){}

    public Person(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}

 

@RequestMapping("testPara2")
    public String testPara2(Person person){
        System.out.println(person.getName()+"------"+person.getAge()+"------"+person.getScore());
        return "index.jsp";
    }

localhost:8888/testspringmvc01/testPara2?name=tom&age=20&score=89.5

你可能感兴趣的:(Java,java,maven,开发语言)