MVC框架入门---SpringMVC(二)

目录

1.SpringMVC如何接收请求参数

1.1接受参数个数少的

 1.2 当表单提交时 是实体类时

 1.3 当网页发送的数据为时间格式时

 1.4 静态资源放行


1.SpringMVC如何接收请求参数

1.1接受参数个数少的

前端jsp代码:             

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/12/4
  Time: 15:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<form action="Login/LoginRequest" method="post">
    账号:<input type="text" name="username" ><br>
    密码:<input type="password" name="password"><br>
    <button>登录</button>
</form>
</body>
</html>

后端Java代码:

package com.zhl.controller;

import com.zhl.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @program: HelloSpringMVC
 * @description:
 * @author: 张会理
 * @create: 2021-12-04 15:27
 **/
@Controller
@RequestMapping("Login")
public class Login {
    @RequestMapping("LoginRequest")
    public String Login(String username,String password){
        System.out.println("username====="+username+"   "+"password===="+password);
        return "/view/MainPage.jsp";
    }
}

注意!前端jsp的input命名要与后端Java命名要一致!!!

 运行结果如下:

 1.2 当表单提交时 是实体类时

把请求的结果封装到一个实体类对象中

首先,要创建一个要接收的实体类对象

package com.zhl.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @program: HelloSpringMVC
 * @description:
 * @author: 张会理
 * @create: 2021-12-04 15:40
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String username;//一定要和请求的参数一致
    private String password;
}

前端代码如下:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/12/4
  Time: 15:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<form action="Login/UserLoginRequest" method="post">
    账号:<input type="text" name="username" ><br>
    密码:<input type="password" name="password"><br>
    <button>登录</button>
</form>
</body>
</html>

后端代码如下:

package com.zhl.controller;

import com.zhl.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @program: HelloSpringMVC
 * @description:
 * @author: 张会理
 * @create: 2021-12-04 15:27
 **/
@Controller
@RequestMapping("Login")
public class Login {
    @RequestMapping("/UserLoginRequest")
    public String UserLogin(User user){//把接受的参数封装到该实体类对象中
        System.out.println(user);
        return "/view/MainPage.jsp";
    }
}

运行结果如下:

 1.3 当网页发送的数据为时间格式时

比如:

我在User实体类中添加一个日期属性

 并修改前端jsp代码  添加一个日期格式的input框

 此时 运行程序  结果如下:

 解决办法:

①:在实体类中添加24进制  代码如下:

package com.zhl.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * @program: HelloSpringMVC
 * @description:
 * @author: 张会理
 * @create: 2021-12-04 15:40
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String username;//一定要和请求的参数一致
    private String password;

    @DateTimeFormat(pattern = "yyyy-MM-dd")//HH:24进制
    private Date birthday;//日期格式
}

②:然后在springmvc的配置文件中添加处理时间类,代码如下:

<?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">

    <!--1.包扫描  扫描我们自己的控制层类所在的包或者父包-->
    <context:component-scan base-package="com.zhl.controller"/>

    <mvc:annotation-driven/><!--处理时间类-->
</beans>

 注意!!!  要选择带有mvc的   不要导错包  否则会失败 

此时,在此运行一次   结果如下:

 1.4 静态资源放行

此时 我们有一个需求  想要在网页上显示一张图片:

 运行结果如下:

 

 我们会发现图片加载不出来 并且浏览器报错 

解决办法 : 放行静态资源

在springmvc的配置文件中添加 放行静态资源

代码如下:

<?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">

    <!--1.包扫描  扫描我们自己的控制层类所在的包或者父包-->
    <context:component-scan base-package="com.zhl.controller"/>

    <!--处理事件类-->
    <mvc:annotation-driven/>

    <!--放行静态资源-->
    <mvc:default-servlet-handler/>
</beans>

此时 我们再来看结果:

 可以看到 图片加载出来了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值