十二、SpringMVC

1. SpringMVC环境搭建

  1. 创建maven---web项目

  2. 补全目录

  3. 添加依赖

  4. 加入tomcat插件

    <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.16</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/testwebproject</path> <port>8849</port> </configuration> </plugin> </plugins> </build>
  5. 创建控制器类,跳转到index.jsp

    @Controller
    public class TestController { //控制器类
    @RequestMapping("/test1")
    public String test1(){
    //响应给前端(浏览器)index.jsp页面:
    return "index.jsp";
    }
    }

  6. 新建Spring MVC框架配置文件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: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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.luo.controller"></context:component-scan>

    mvc:annotation-driven</mvc:annotation-driven>
    </beans>

修改web.xml配置

复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"> 
</web-app>
  1. 编写web.xml内容

    <?xml version="1.0" encoding="UTF-8" ?>

    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
    <servlet>
    <servlet-name>springmvc</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>springmvc</servlet-name>

    <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>

2. SpringMVC获取普通参数

获取普通参数,只需要在控制单元中提供与请求参数同名的方法参数即可,Spring MVC会自动进行类型转换。

复制代码
    @RequestMapping("/testParam")
    public String testParam(String name, int age){
        System.out.println(name+"---"+ age);
        return "index.jsp";
    }

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

JavaBean:一个包含私有属性,getter/setter方法和无参构造方法的Java类,是不是感觉和实体类特别像。其实写法上和实体类相同。唯一区别是实体类是数据库层面的概念,类型中属性要和数据库字段对应。而JavaBean的属性是灵活的,不是必须和哪里对应的。

JavaBean是一个专业概念,可以简单点理解:使用类对象做为控制单元参数,接收请求参数,如果不是特别较真,狭义上可以认为JavaBean就是项目中的实体类。

在控制单元中放置一个类型对象,对象名称没有要求,只需要保证请求参数名和类的属性名相同就可以了。

复制代码
public class Person {
    private String name;
    private int age;
    private double 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;
    }

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

    @RequestMapping("/testParam2")
    public String testParam2(Person p){
        System.out.println(p.getName()+"---"+ p.getAge()+"---"+p.getScore());
        return "index.jsp";
    }
相关推荐
小许学java20 小时前
Spring事务和事务传播机制
java·数据库·spring·事务
这是程序猿1 天前
基于java的ssm框架经典电影推荐网站
java·开发语言·spring boot·spring·经典电影推荐网站
jiayong231 天前
海外求职平台与策略指南
java·spring
SadSunset1 天前
(37)全注解式开发AOP
java·spring
子超兄1 天前
Bean生命周期
java·spring
Mr.朱鹏1 天前
超时订单处理方案实战指南【完整版】
java·spring boot·redis·spring·rabbitmq·rocketmq·订单
Lisonseekpan1 天前
RBAC 基于角色的访问控制模型详解与实践指南
java·服务器·网络·后端·spring·log4j
while(1){yan}1 天前
计算器和登录界面(实现前后端互通)
spring boot·spring·servlet·java-ee·tomcat·maven
爱吃山竹的大肚肚1 天前
Spring Boot 与 Apache POI 实现复杂嵌套结构 Excel 导出
java·spring boot·后端·spring·spring cloud·excel
SadSunset1 天前
(35)使用Spring的AOP
java·数据库·spring