SpringMVC_2023_11_28_1 SpringMVC_进阶(数据的接收)

发布时间 2023-11-29 10:51:26作者: Kbaor

SpringMVC进阶

2023-11-29 10:13:40 星期三

数据的接收

a)页面传递的属性名和后台接受的属性名不一致时,如何处理?

@RequestParam("页面组件中的属性名")
    @RequestMapping(value = "/login",
            method = RequestMethod.POST)
    public String login(@RequestParam("uname") String name,
                        @RequestParam("upasswd") String password){
        System.out.println(name+"-"+password);
        return null;
    }

Spring:MVC命名空间

<!--
       springmvc注解的驱动
       默认创建:RequestMappingHandlerMapping、RequestMappingHandlerAdapter
   -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="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.xsd
        http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--
        springmvc注解的驱动
        默认创建:RequestMappingHandlerMapping、RequestMappingHandlerAdapter
    -->
    <mvc:annotation-driven/>
    <!--
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
    -->
    <context:component-scan base-package="com.neuedu.controller"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

</beans>

UserController

package com.bboy.controller;

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;

/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/29 0029 10:17:17
 */
@Controller
public class UserController {
    @RequestMapping(value = "/login",
            method = RequestMethod.POST)
    public String login(@RequestParam("uname") String name,
                        @RequestParam("upasswd") String password){
        System.out.println(name+"-"+password);
        return null;
    }
    @RequestMapping(value = "/test",
            method = RequestMethod.GET)
    public String test(int id,String name){
        System.out.println("===>test"+id+":"+name);
        return "/WEB-INF/show.jsp";
    }
    @RequestMapping("/test01/{id}/{name}")
    public String test01(@PathVariable("id") int id,
                         @PathVariable("name") String name){
        System.out.println("===>test01"+id+":"+name);
        return "/WEB-INF/show.jsp";
    }
}

index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>JSP - Hello World</title>
</head>
<body>
<form action="/login" method="post">
    用户名:<input type="text" name="uname"/><br/>
    密码:<input type="text" name="upasswd"/><br/>
    <input type="submit" value="登录"/>
</form>
<a href="/test?id=10&name=tom">测试</a><br/>
<a href="/test01/11/jack">测试-1</a><br/>
</h1>
<br/>
</body>
</html>

image
image