Static properties when using specific bean instances in JSPs
I've inherited some JSP code that I'm working on cleaning up, based on the warnings/errors being given in Eclipse. One of the common warnings is about a practice they've been using to centralize configuration of the input field names, which I'm given to understand is common. They're using statically declared strings in the beans. So for example, near the top we have the specific instance of the bean being defined and used:
<jsp:useBean id="myBean" scope="session" class="com.company.package.MyBean" />
<%
String[] sequence = myBean.getSequence();
HashMap statusLabelMap = myBean.getStatusLabels();
%>
but then later on, static properties from the bean's class are also used, in a non-static way:
<label for="<%=myBean.MOD_START%>">Start With:</label>
<select name="<%=myBean.MOD_START%>" id="<%=myBean.MOD_START%>">
Here's the declaration for MOD_START in the bean itself:
public static final String MOD_START = "modStart";
Clearly the correct way to access MOD_START is MyBean.MOD_START, not myBean.MOD_START. However,开发者_开发技巧 given the way the bean instance is being created, this syntax doesn't work. I could add:
<%@ page import="com.company.package.MyBean" %>
and then change the static property accesses. But me being a relative lightweight for JSP coding, I'm wondering if this is the "most correct" way to address this problem. Or should I just let it go and leave the accesses using the non-static syntax? I'd appreciate any guidance you can offer on best practices to cover this case.
精彩评论