1

I have a string which looks like this and it represents a pojo.

Model [Name=Mobie , location= US, actualTransferDate=null, scanserialCode=234335,1237787, modelNum=MIC 898989 ]

I want bit clearer to reader on the above string. I want to read the user checked checkbox values(represents entire row with the fileds in below pojo) in an jsp page table to another jsp page. So, in the controller i read these checked checkbox rows as bellow.

String[] checkeditems = request.getParameterValues("case");//case represents the entire row
    for (String string : checkeditems) {
        log.info("row1"+string);// String pasted above in the message
    }

From the above it returns as a string Array which i want convert to be as a list object, so that i can easily send this list to next jsp for a view. I feel i am heading to wrong direction and doing some unrelated stuff.

I have a pojo as

public class Model{
private String Name;
private String location;
private String actualTransferDate;
private String scanserialCode;
private String modelNum;
====Getters/Setter======

How i can convert this String to this model object?

1
  • Can you point any example or bit more write help to understand on this? Commented Mar 22, 2015 at 9:23

3 Answers 3

1

you can split the string on ", " and iterate over the result array. With BeanUtils from apache can you fill your new pojo instance.

Example:

public class Model {
    private String Name;
    private String location;
    private String actualTransferDate;
    private String scanserialCode;
    private String modelNum;
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    public String getActualTransferDate() {
        return actualTransferDate;
    }
    public void setActualTransferDate(String actualTransferDate) {
        this.actualTransferDate = actualTransferDate;
    }
    public String getScanserialCode() {
        return scanserialCode;
    }
    public void setScanserialCode(String scanserialCode) {
        this.scanserialCode = scanserialCode;
    }
    public String getModelNum() {
        return modelNum;
    }
    public void setModelNum(String modelNum) {
        this.modelNum = modelNum;
    }
    @Override
    public String toString() {
        return "[Name = " + getName() + "location = " +getLocation() + ", actualTransferDate = " + getActualTransferDate() + ", scanserialCode = " + getScanserialCode() + ", modelNum = " + getModelNum() + "]";
    }
}


import org.apache.commons.beanutils.BeanUtils;

public class Main {

    public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {
        String model = new String("Name=Mobie , location= US, actualTransferDate=null, scanserialCode=234335,1237787, modelNum=MIC 898989");

        String[] modelValues = model.split(", ");

        Model m = new Model();

        for (String value : modelValues) {
            String[] s = value.split("=");

            String fieldName = s[0];
            String fieldValue = s[1];

            BeanUtils.setProperty(m, fieldName, fieldValue);
        }

        System.out.println(m.toString());
    }
}

Maven dependency:

<dependencies>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.2</version>
    </dependency>
</dependencies>
Sign up to request clarification or add additional context in comments.

Comments

0

If you want it completely dynamic, you can use Reflection.

For example, use a regular expression (Pattern/Matcher) to find the [ ... ] part, use the String before that as a class name (assuming you know the package name) and then do a simple comma/equals-sign split in the [ ... ] part and fill the fields via reflection... Not that hard to do.

Comments

0

You can define a constructor in the Model class which accepts the full string as input. The use StringTokenizer with delimiter as ',' to convert the string to a list of tokens. Then tokenizer each token with '='as the delimiter. This way you will have all the members of Model class tokens which can be used to initialize the values of the member variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.