Enum Usage Tips

Source: Internet
Author: User

Recently in learning Spring MVC, there are jquery-easyui and other frameworks, so by the way I set up a framework to play, because I am more lazy, and love the blind toss, so there are the following thinking.

Many people now design the database will be such as " male and female "," Enable, disable " and other commonly used Combination of saving to the data when you choose not to save the text and chose to use or other state to replace , that is, 0 for the male, 1 for the female, so as to save the database storage, the benefits of nature is self-evident, I also take this approach when I do projects in the company.

But also because they have experienced in the company, so only deep feeling the pain of this way, for this common combination, when used on the page is naturally to the user a group of radio to choose, and then get 0,1 after saving . In front of the time and re-based on the value of the display, I am a lazy person, not so much fun to do so many meaningless things, and sometimes design confusion, some places may not be able to achieve unity, the male design for 1, female 0(some evil ideas came out, Here the connotation, do not understand please drift over) is very troublesome, so began to think change.

In an accidental opportunity, a colleague in the office said Java since the XXX version (I do not remember) support Chinese variables, but also this sentence let me start to active, I think of another piece of the sky: Enum. So a class was defined:
Public enum Gender {
male , female
}

It works well and defines two radio on a page

<input type= "Radio" name= "Sex" value= "male" >

<input type= "Radio" name= "Sex" value= "women" >

Spring MVC can be converted automatically when value-to-type conversions, and there is a corresponding memo in the JPA specification that defines the storage order rather than the display value, thus realizing the previous idea, without requiring

@Enumerated (value = Enumtype.) ORDINAL)
@Column (length=1)
Public Gender Getsex () {
return sex;
}

After the test is smooth, the page display is not a problem, the data save is very good, but there is a small problem: If you enable the disabled this set of data, some people like to enable the police, some people like to enable the non-enabled, in the project is difficult to unify, plus for me lazy person, need to continue to play multiple <input > tag is also a nuisance, so there are some thoughts: can directly in the foreground of the time directly through Ajax to get all the fields needed to display, and then generate a set of <input tags, think of the start.

So I wrote one such a method:

The passed classpath is the full path of the enum that needs to be invoked.

PublicList<string> getfromenum (String classPath) {
Try{
intID =0;
list<string> result =NewArraylist<string> ();
Class u = class.forname (ClassPath);
for(Field f:u.getfields ()) {
Result.add (F.getname ());
}

returnResult

}Catch(Exception e) {
E.printstacktrace ();
return newArraylist<string> ();
}
}

So the front desk can only call Ajax to get this array, and then to display, you can avoid the problem of nonstandard.

The previous article has always stressed that I am a lazy person, as a lazy person can not write the Ajax method every time, and then regenerate, naturally have to wrap up, so simply write a JSP, Ajax calls and processing unified writing, so that the completion of my lazy road. Of course, lazy is not so easy, such as someone in the design of the time like to use radio, some people like the ComboBox, many times also according to different contexts need different design, this JSP file seems to become quite important, need to consider a variety of situations, now not perfect on the first not posted out, recently will be perfected , and then post it to share.

In addition, there is another idea is if the ordinal method of the Enum can be rewritten, so that the stored value can not start from 0, but re-designed to 1001,1002, etc. Of course, this is just some miscellaneous thinking, in fact, when rebuilding a system should not use this.

Enum Usage Tips

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.