首页 > 解决方案 > SPRING MVC:获取从表单返回的键值:选择

问题描述

我在课堂上学习 Spring MVC,并且一直想知道为什么我的表单选择发送我的地图的键而不是选择的值。

这是显示:

表单输入 -

表单输入

显示结果 -

显示结果

这是我的代码:

控制器 :

package com.mycompany.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import entity.User;

@Controller
public class DefaultController {
    
    @Value("#{countries}")
    private Map<String, String> listCountry;
    
    @GetMapping("/")
    public String showHome()
    {
        return "home";
    }
    
    @GetMapping("needForm")
    public String displayForm(Model model)
    {
        for (Map.Entry<String, String> entry : listCountry.entrySet()) {
            System.out.println(entry.getKey() + "/" + entry.getValue());
        }
        model.addAttribute("user", new User());
        model.addAttribute("countries", listCountry);
        return "userForm";
    }
    
    @PostMapping("processForm")
    public String filledForm(@ModelAttribute("user") User user)
    {
        System.out.println(user.getCountry());
        return "viewForm";
    }

}

实体 :

package entity;

public class User {
    
    private String firstName;
    private String lastName;
    private String country;
    private int age;
        
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String pays) {
        this.country = pays;
    }
    
}

country.properties 文件

FR = France
IT = Italie
ES = Espagne
RU = Russie
BE = Belgique

第一个 JSP 发送参数(userForm):

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" 
uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>user form</title>
</head>
<body>

    <form:form action="processForm" modelAttribute="user">
        First Name :    <form:input path="firstName" /> <br><br>
        Last Name :     <form:input path="lastName" /> <br><br>
        Age :           <form:input path="age" /> <br><br>
        Pays :
        <form:select path="country">
            <form:options items="${countries}"/>
        </form:select>
        <input type="submit" value="valider"/>
    </form:form>
    
</body>
</html>

第二个 JSP 获取参数(viewForm):

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>viewForm</title>
</head>
<body>
User:<br>
<br>
First name : ${user.firstName}<br>
<br>
Last name : ${user.lastName}<br>
<br>
Age : ${user.age}<br>
<br>
pays: ${user.country}
</body>
</html>

标签: spring-mvc

解决方案


推荐阅读