[英]Spring form — modelAttribute name as java constant
in order to not repeat myself in code I've used modelAttribute
name as java constant 为了不重复我的代码,我使用
modelAttribute
名称作为java常量
@Controller
@RequestMapping("/")
public class Controller {
public static final String MODEL_ATTRIBUTE = "myModel";
public String renderPage(Model model) {
model.addAttribute(MODEL_ATTRIBUTE, ...);
return "index";
}
}
Now I import this constant into JSP using scriptlet (I know that scriptlets are bad, but I'm not familiar with better solution). 现在,我使用scriptlet将这个常量导入JSP(我知道scriptlet不好,但是我不熟悉更好的解决方案)。
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@page import="static com.example.Controller.*" %>
<form:form action="/" modelAttribute="<%= MODEL_ATTRIBUTE %>">
<form:label path="attr1">Attribute:</form:label>
<form:input path="attr1" />
<c:forEach items="${???.attr2}" var="item">
...
</c:forEach>
<input type="submit" />
</form:form>
How should I refer to modelAttribute
object in the forEach loop? 我应该如何在forEach循环中引用
modelAttribute
对象? Is there better solution for DRY in Spring forms? Spring形式中的DRY是否有更好的解决方案?
Use the model name, 使用型号名称,
<c:forEach items="${myModel.attributeNameFromYourModel}" var="item">
...
</c:forEach>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.