Error setting value method "setage" failed for object... ljava. Lang. string error Summary

Source: Internet
Author: User
Error Setting Value Method "setage" failed for object... ljava. Lang. string error Summary

(11:01:46)

Reprinted token
Tags:

Struts

 

Error

 

Setting

 

Value

 

Null Value

 

Submit

 

Parameters

 

It
 

The following should be some of my friends who often encounter errors when using struts2.1 or a later version for demo.

Error Setting Value
Ognl. methodfailedexception: Method "setage" failed for object com. Test. Action. registeraction @ d6b059 [java. Lang. nosuchmethodexception: setage ([ljava. Lang. String;)]

 

The following uses age as an example. In xxaction and the corresponding foreground page, there is a region named "age ".

Xxaction includes: Private int age;

Case Description: When a blank form value is submitted, struts2 should automatically convert the submitted age segment of the string type to the int type value we declare as expected. When we submit a null value, the default value is 0. if the value is invalid, such as ABC,-1, or 500, it should be set by xxaction. properties and the validate () method in xxaction to verify.

 

Actual situation: in fact, when we submit a null value, the above error message "error setting value" appears. Even after the submission, the corresponding region can still be set to 0, however, such error prompts often make us uncomfortable.

 

Situation Analysis: Let's analyze the process below. (Some reference to analysis by netizens)
The struts2 process is:
Obtain the client request parameters ---> directly convert to the data type we want (this step may cause exceptions during the transformation because no verification is available) ------> check whether the parameters are valid.

 

In struts2.1x or later versions, struts2 considers "" as a null string rather than null, so ljava will occur. lang. string type. If a friend is interested in executing such a piece of code:

System. Out. println (string []. Class );

The printed result is:

Ljava. Lang. String

That is, the empty area is considered as a string, so a warning is reported when you convert the type (string [] to int.

 

Here, I provide a method to eliminate the warning temporarily, that is, when declaring the age, declare it:

Private string age;

The corresponding get set method is:

 

 Public String getage (){
  Return age;
 }
 Public void setage (string age ){
  If ("". Equals (AGE) | age = NULL ){
   This. Age = "0 ";
  } Else {
   This. Age = age;
  }
 }

 

So some friends will say this is a pitfall. There is no need to manually convert the convenient type conversion of struts2. Isn't it a nerd --.

There is a certain truth, but the framework is just a means. If we cannot control each step by ourselves, but only rely on it, then when the program really has a big error, it is difficult to find the cause of the error.

 

Next, we will analyze some features of struts2:

1. In action, if all the variables that receive the parameters transmitted by the client are of the string type, the program will be fine. However, this means that the program does not use the built-in type conversion mechanism of struts2. Because the data transmitted by the client is originally string.

2. parameters such as integer, double, date are defined in the action to receive parameters submitted by the client. However, if the format of parameters submitted by the client is incorrect, an error is reported in the background.

 

In addition, we will rewrite all the types of converters we need, and then convert them into one. If an exception occurs, we will catch it and generate fielderror, and then call input. This is also a way to solve this problem.

 

To sum up: when a friend encounters some problems, they can use some solutions that suit their habits. The framework is a tool after all. If some solutions do not meet their own expectations, they simply do not need some features.

These are my feelings and hope to help you.

 

 

============================== Lili split line ====================== ================================

After another experiment, the above solution is valid for correct input, but errors such as character-to-number conversion may occur.

Therefore, although there will be background alarms, you can declare the corresponding type of the variable without affecting the use of the variable, and the number is declared as Int or floating point.

Transfer address http://blog.sina.com.cn/s/blog_69b9434b01012ke1.html

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.