How do you use constants to prevent hard coding in JSP (JSTL)?

Source: Internet
Author: User
Tags interface
js| Code

In general, it is a bad habit to write hard code in a program, such as: this.state= "01"; The usual practice is to write a class (interface) to hold the constants
public interface Systemconstant
{
String STATE_WTJ = "01";
}

And then you can write that in the program.
THIS.STATE=SYSTEMCONSTANT.STATE_WTJ;


In a Java program, you can avoid hard coding. But what about the JSP? Of course, if the JSP allows the use of scriplet, of course, can also directly use the constant, but now JSP generally do not allow the appearance of <%%> such code, such as in the Jstl how to do?

<c:if test=${state== '}> '

</c:if>

So there's a hard code like ' 01 '. Just saw the Appfuse solution, feel very good.

First write a jsptag, see Org.appfuse.webapp.taglib.ConstantsTag class in detail.

And then use this tag in the JSP.
<appfuse:constants scope= "Request"/>

In this way, Jstl can be written in this way:
<c:if test=${state==state_wtj}>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.