One article handles the casting of Struts2

  • 2020-06-03 06:36:14
  • OfStack

preface

We know that all the data that we submit to the background through HTTP is in the form of a string, and we need more than just a string type of course. So, we need type conversions! Let's take a look at the details below.

1. The meaning of type conversion

All MVC frameworks, which address presentation layer solutions, need to collect user request parameters and provide them to the controller. However, in the Web application, all request parameters are string type. Java is a strongly typed language. How to convert strings to Java is a problem that the framework has to solve.
For example, a registration page requires entry of age, date, and so on. The date and age strings need to be converted to the appropriate type.

2. Struts2 type converter

Struts2 provides built-in type converters. For most types, developers do not need to customize type converters, but use built-in type converters to do the conversion we need.

The built-in type converters are:

1. boolean and Boolean

2. char and Character

3. int and Integer

4. long and Long

5. float and Float

6. double and Double

7.date

Let's say we type 20 on the page, and it's going to be a string in the background. But the built-in cast of the Struts framework can convert 20 to int. But if we convert something like abc to int it won't work.

3. Implementation of custom type converters

1. Demand analysis

For example, we need to enter (2,3) such a coordinate point on the page to convert it to a point object type.

2, implementation,

Custom conversions are needed when built-in conversions do not meet our needs. The type conversion of Struts2 is based on ONGL. There is an interface TypeConverter in ONGL and a method converterValue in the interface, which is very complicated. There is also a default implementation class, DefaultTypeConverter, for type conversion.


package com.study.converter;
import java.util.Map;
import com.study.bean.Point;
import ognl.DefaultTypeConverter;
 
public class PointConverter extends DefaultTypeConverter{
 public Object convertValue(Map context, Object value, Class toType) {
  if(toType==Point.class){
   String []strs = (String[])value;
   String []xy = strs[0].split(",");
    
   int x = Integer.valueOf(xy[0]);
   int y = Integer.valueOf(xy[1]);
    
   Point p = new Point();
   p.setX(x);
   p.setY(y);
    
   return p;
    
  }else if(toType==String.class){
   Point p = (Point) value;
   return "["+p.getX()+","+p.getY()+"]";
  }else{
   return null;
  }
 }
}

This is a simple implementation of the converter, which parses the value of the page transfer to the Point type. Then you need to configure it for the type converter to work
struts2 provides two configuration methods:

a. Local: First create a file named Actionname-ES96en. properties, where the content is property = type converter ( ponit=com.study.converter ) It needs to be written under the package

b. Global: First create a file named xwork-ES104en.properties with the contents as com.study.bean.Ponit=com.study.converter . The file needs to be written under src

3. Implementation of Struts

There is an StrutsTypeConverter class in struts2, which inherits DefaultTypeConverter. It is more convenient to customize the converter using it, as shown in the following example code:


package com.study.converter;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
import com.study.bean.Point;
 
public class PointConverter extends StrutsTypeConverter{
 public Object convertFromString(Map map, String[] obj, Class toType) {
  String []strs = obj;
  String []xy = strs[0].split(",");
 
  int x = Integer.valueOf(xy[0]);
  int y = Integer.valueOf(xy[1]);
 
  Point p = new Point();
  p.setX(x);
  p.setY(y);
 
  return p;
 }
 
 public String convertToString(Map map, Object obj) {
  Point p = (Point) obj;
  return "["+p.getX()+","+p.getY()+"]";
 }
}

4, type converter error handling

Struts2 provides an interceptor called conversionError that encapsulates the error as a form error (filederror) and puts the error message into ActionContext if a conversion error occurs.

How do I change the error message for the default cast error?

Method 1:

First create a resource file, Mess.properties, under src
Then cover xwork core - 2.3.31 \ com \ opensymphony \ xwork2 \ xwork - messages properties attribute xwork inside. default. invalid. fieldvalue. Write the following parameter in ES153en.properties


xwork.default.invalid.fieldvalue="{0}" Parameter conversion failed .

Then specify our resource file in struts.xml (no file suffix required)


<constant name="struts.custom.i18n.resources" value="Mess"></constant>

Method 2:

Provides an error message prompt for an action field
Define Actionname. properties file, add configuration -- Invalid. fieldvalue. attribute name = prompt message

A total of

The above is the whole content of this article, I hope the content of this article can bring 1 definite help to your study or work, if you have any questions, you can leave a message to communicate.


Related articles: