gpt4 book ai didi

spring-mvc - 关于 spring @ModelAttribute 接受任意字符串的问题

转载 作者:行者123 更新时间:2023-12-01 09:12:40 24 4
gpt4 key购买 nike

在 spring3 上课时,我从教程中编写了一个示例。我创建了一个 Controller ,如下所示

package my.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import my.spring.form.Contact;

@Controller
public class ContactController {
@RequestMapping(value ="/addContact",method =RequestMethod.POST)
public String addContact(@ModelAttribute("contact") Contact ct){
System.out.println("First Name:" + contact.getFirstname() + "Last Name:" + contact.getLastname());
return "redirect:contacts.htm";
}
@RequestMapping("/contacts")
public ModelAndView showContacts() {
System.out.println("showing contacts");
return new ModelAndView("contact", "userEntries", new Contact());
}
}

然后我决定尝试一下并修改方法参数中的@ModelAttribute

public String addContact(@ModelAttribute("contact") Contact ct)

public String addContact(@ModelAttribute("somevalue") Contact ct)

我仍然找不到应用程序行为的任何变化。这对我来说有点意外。据我了解,表单中的数据收集在 Contact 对象中,并使用 @ModelAttribute 该对象绑定(bind)到参数 ct。然后使用该参数在方法内部进行处理。@ModelAttribute() 内部使用的实际字符串是否无关紧要?

这是 WEB-INF/jsp/contact.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Contact Manager</title>
</head>
<body>
<h2>Contact Manager</h2>
<form:form action="addContact.htm" commandName="userEntries">
<table>
<tr>
<td>
<form:label path="firstname">First Name</form:label>
</td>
<td>
<form:input path="firstname"/>
</td>
</tr>

<tr>
<td>
<form:label path="lastname">Last Name</form:label>
</td>
<td>
<form:input path="lastname"/>
</td>
</tr>

<tr>
<td colspan="2">
<input type="submit" value="Add Contact"/>
</td>
</tr>
</table>

</form:form>
</body>
</html>

和 spring servlet 配置

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="my.spring.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

最后,index.jsp 转发给联系人

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>my index</title>
</head>
<body>
<jsp:forward page="contacts.htm"></jsp:forward>

</body>
</html>

最佳答案

它将用于将模型属性映射到表单:

<form:form modelAttribute="myObject">

</form>

您必须使用

public String controllerMethod(@ModelAttribute("myObject") Object obj){
....
}

如果您不在两个模型属性值中使用相同的值,您将无法看到验证的错误消息(在 Controller result.rejectValue 和 jsp <form:errors/> 标记中执行操作时)。

在对象中填充表单中的值没有区别,因为您可以使用 html 标签而不是 spring 标签,并且在 html 标签中您只有 name 属性来映射值。

如果您在如下方法中使用@ModelAttribute,您将在模型中插入一个名为“myObject”的属性,其值是对象返回的值。这是此注释的另一个功能,它将在 Controller 的任何方法之前调用。

@ModelAttribute("myObject") Object obj
public Object method(){
...
return obj;
}

关于spring-mvc - 关于 spring @ModelAttribute 接受任意字符串的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4525253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com