【问题标题】:Proper use of Spring tag library in jsp在jsp中正确使用Spring标签库
【发布时间】:2014-12-10 10:02:13
【问题描述】:

如果我不使用<% @ taglibprefix = "sf" uri = "http://www.springframework.org/tags/form"%> 该应用程序的工作原理相同。 User 用户对象填充了表单的字段。使用这种方法是否正确?

<sf:form method="POST"modelAttribute="user">的使用是否更正确?

<%@ 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>Inserisci nuovo utente</title>
</head>
<body>
<h2>Dati utente</h2>
<form action="/SpringMVCFormHibernate/add" method="post">
    <label>Cognome</label><br/><input type="text" name="cognome"/><br/>
    <label>Nome</label><br/><input type="text" name="nome"/><br/>
    <label>Eta</label><br/><input type="text" name="eta"/><br/><br/>

    <input type="submit" value="submit"/>
</form>
<p><a href="/SpringMVCFormHibernate/show">Visualizza utenti</a></p>
<sf:label path=""></sf:label>
</body>
</html>

@Controller
public class UtenteController {

@Autowired
UtenteDAO utenteDAO;

@RequestMapping(value="/add",method=RequestMethod.POST)
public String addUtente(@ModelAttribute Utente user){
    utenteDAO.inserisciUtente(user);
    return "index";
}//addUtente
}//UtenteController 

【问题讨论】:

  • User 用户对象填充了表单的字段。使用这种方法是否正确 没有表单标签你怎么办。你在用 jstl 吗?
  • 附注:请考虑使用UTF-8 而不是ISO-8859-1
  • 是的,我正在使用 jstl,但表单没有指定模型属性..spring 表单是的。

标签: spring jsp spring-mvc model-view-controller


【解决方案1】:

spring:form 标签的主要用途是formbacking 对象。如果您希望将模型属性对象与视图字段绑定,则可以这样做。

对于简单的表单对象,您可以使用 html 表单。你也可以使用spring:form标签错误属性。

对于

path 属性绑定模型字段名。因此对它们所做的更改可以在服务器端使用您的模型属性轻松更新。

它们很容易提供对象的动态绑定。 spring 做这些而不是手工工作

nice example 了解表单处理和模型属性的使用。

【讨论】:

  • html 表单没有指定模型属性..但是 User 用户对象填充了表单的字段..为什么?
  • 因为模型属性是 request 。所以它是可用的
  • 但是,例如,如果我有两个带有两个表单的 jsp,他们如何确定绑定哪个对象?如果 html 表单没有指定模型属性,则第一个表单的参数可以与错误的项目
  • modelAttribute 决定它:)
  • 那么html表单和spring表单没有区别吗?都是实现绑定的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-01
  • 1970-01-01
  • 2011-04-02
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
相关资源
最近更新 更多