使用枚举来检索页面 bean
考虑以下两个示例:
<c:if test="${errorMessage!=null}">
Welcome <c:out value="${homePageBean.currentUserName}"/>
当我查看 Spring 控制器与支持 JSTL 的 JSP 通信的示例时,我注意到程序员在控制器中硬编码模型对象名称,然后在 JSP 中硬编码相同的名称。我正在努力寻找更好的方法来做到这一点。
我创建了一个包含模型对象名称的枚举。
public enum Bean {
ERROR_MESSAGE("errorMessage"),
LOGIN_PAGE_BEAN("loginPageBean"),
HOME_PAGE_BEAN("homePageBean");
private String beanId;
private Bean(String beanId) {
this.beanId=beanId;
}
public String getBeanId() {
return beanId;
}
因此
,在我的控制器中,我没有硬编码对象名称,而是简单地执行以下操作:
String errorMessage=failedAttempt?"login.invalidUserNameOrPassword":null;
model.put(Bean.ERROR_MESSAGE.getBeanId(), errorMessage);
但无论我如何尝试,我似乎都无法使用 Bean.ERROR_MESSAGE 在 jsp 中检索此“bean”。 getBeanId()。
<c:out value="${errorMessage}" /> // works but uses hardcoded name
<c:out value="${Bean.ERROR_MESSAGE.getBeanId()}" /> // doesn't work
<c:out value="${<%=Bean.ERROR_MESSAGE.getBeanId()%>}" /> // doesn't compile
有人有解决办法吗?
Consider the following two examples:
<c:if test="${errorMessage!=null}">
Welcome <c:out value="${homePageBean.currentUserName}"/>
When I look at examples of Spring controller to JSTL-enabled JSP communication, I notice that programmers hardcode the model object name in the controller and then hardcode the same name in the JSP. I'm trying to find a better way to do this.
I created an enum that contains the names of model objects.
public enum Bean {
ERROR_MESSAGE("errorMessage"),
LOGIN_PAGE_BEAN("loginPageBean"),
HOME_PAGE_BEAN("homePageBean");
private String beanId;
private Bean(String beanId) {
this.beanId=beanId;
}
public String getBeanId() {
return beanId;
}
}
So in my controller, instead of hardcoding object name, I simply do this:
String errorMessage=failedAttempt?"login.invalidUserNameOrPassword":null;
model.put(Bean.ERROR_MESSAGE.getBeanId(), errorMessage);
But no matter how I try, I can't seem to be able to retrieve this "bean" within the jsp by using Bean.ERROR_MESSAGE.getBeanId()
.
<c:out value="${errorMessage}" /> // works but uses hardcoded name
<c:out value="${Bean.ERROR_MESSAGE.getBeanId()}" /> // doesn't work
<c:out value="${<%=Bean.ERROR_MESSAGE.getBeanId()%>}" /> // doesn't compile
Does anyone have a solution to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将必须引用相同的“标识符”两次,无论是硬编码的
String
、类名还是其他任何内容。一次进入控制器,一次进入视图。我看到您正在尝试采用更加编程的方法,理论上这是一个好主意,但您最终必须在 JSP 中注入 scriptlet 才能实现这一点。除非必要,最好避免这种情况。
我建议在您的控制器类中使用一些定义良好的常量(这样您就不会在 Java 代码中硬编码
String
)并坚持引用这些String
在您的 JSP 中按名称(正如您当前所做的那样)。这是大多数 MVC 应用程序使用的常见方法。You're going to have to reference the same "identifier" twice, be it a hard-coded
String
, a class name, or whatever else. Once in the controller and once in the view.I see where you are trying to take a more programmed-approach, and it theory it's a good idea, but you end up having to injecting scriptlets in your JSP to make this happen. It's best practice to avoid this unless necessary.
I'd recommend some well defined constants for use in your controller classes (so you're not hard-coding
Strings
all over your Java code) and stick to referencing thoseString
s by name in your JSPs (as you're currently doing). This is the common approach, used by the majority of MVC applications.