Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that date type variables can not be defined in Struts form

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to solve the problem that date type variables can not be defined in Struts form. It is very detailed and has certain reference value. Friends who are interested must read it!

In practice, I found that Struts form really can't even deal with some simple time (I don't know if I'm using the wrong method or Struts form really doesn't take it into account). By the way, you can also see how Struts populate the request parameters in form to ActionForm.

This afternoon my colleague told me that there was an error in storing a class with a java.util.Date type attribute in the database, so there will be no problem with deleting this attribute. At that time, I thought that RequestProcessor had made a mistake in processPopulate (), so I set a breakpoint in its method and tracked it in. Of course, it * calls the reset () method of ActionForm, and then calls the RequestUtils.populate () method that actually handles populate (passing request parameters to ActionForm). This static method of RequestUtils * * deals with Multipart (that is, multiple parts such as file upload), and then puts all requests in a HashMap called properties and loops it:

Names = request.getParameterNames (); while (names.hasMoreElements ()) {String name = (String) names.nextElement (); String stripped = name;if (prefix! = null) {if (! stripped.startsWith (prefix)) {continue;} stripped = stripped.substring (prefix.length ());} if (suffix! = null) {if (! stripped.endsWith (suffix)) {continue;} stripped = stripped.substring (0, stripped.length ()-suffix.length ()) } if (isMultipart) {properties.put (stripped, multipartParameters.get (name));} else {properties.put (stripped, request.getParameterValues (name));}}

They are actually handled by the following: BeanUtils.populate (bean, properties); where bean is the ActionForm that accepts data, and properties contains all the requested key-value pairs (keys and values are strings, characteristic of the http protocol).

Let's take a look at how BeanUtils's static (class) method populate handles it:

/ / Loop through the property name/value pairs to be setIterator names = properties.keySet () .iterator (); while (names.hasNext ()) {

It loops through all the request parameters, leaving the actual work to the setProperty method. Ha ha, after a long time, these people turned out to be agents.

Is this method still an agent? It is calculated that it has 180 lines of code. You should be a doer for such a long time, wrong! Never be deceived by the appearance of some people! Some people work 16 hours a day, but they are dedicated enough to play CS for 8 hours. This class is: more than 20 lines are in an if (log.isTraceEnabled ()) {}.

Log is here to explain. Jakarta Commons Logging package is used in Struts form, and its priority is: Log4j (4 pronunciation of four seems to make sense, probably means Logger For Java, I hear that some people feel awkward when they are Log si J, hehe), Java 1.4 Logging API,Simple Logging. The function is weakened in turn.

It is recommended to use Commons Logging instead of System.out.println () in execute () that writes Action or in business methods called by execute ()-- you'll think Commons Logging is a great thing when you're asked to remove hundreds of System.out.println (). Its usage is:

Import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;private/protected static Log log = LogFactory.getLog (DispatchAction.class)

If you are using DispatchAction, you should not define an instance of Log yourself, because it already has an instance of Log of protected, so you can use it directly.

How to use Struts form is as follows:

If (log.isInfoEnabled ()) {log.Info ("some information.");}

Logging divides messages into six levels, debug,error,fatal,info,trace,warn. For example, if you want to log a message just to give a warning to the user, you can use warn. Why make a judgment before each log.Info ()? Can you still Info if Info,log.Info () is not allowed at the log level? Of course not. Its function is to improve efficiency.

For example, there is a message that calculates the sum of the first ten thousand natural numbers (this kind of news may be rare). Use direct log.Info ()

Int sum=0;for (int iTuno Bandi)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report