spring-mvc - 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>
解决方案
推荐阅读
- flyway - FlyWay 是否支持 Sybase IQ 数据库?
- javascript - 从 div 中显示的 mySQL 内容中自动填充表单隐藏字段
- perl - 连接到 DBIx::Class 数据库而不重复连接细节?
- sql - 使用数字字符串作为 SqlParameter 到带有 IN 运算符的 SQL 查询资源
- java - 什么是原始类加载器?它是如何在 Java 中使用的?
- java - Android Studio NO image is seen in Image-Layout over Grid Layout
- php - 可以在对象中动态添加特征吗?
- c++ - 如何在命名空间中正确调用此函数?C++
- amazon-web-services - CNAME 条目在使用 Amazon Certificate Manager 的 NameCheap 上不起作用
- python - PyTorch 线性回归问题