How to get foreign keys, using Hibernate Annotations, Struts 2 and JSP

ぐ巨炮叔叔 提交于 2019-11-29 17:36:50

what you need is a converter. An xwork converter. This is how you go about it. Let's say that your state object is this

public class State implements Serializable
{
     private String stateName;
     private Long stateId;
     //getters and setters
}

Now do this. In your action class, add state as a property

public class CadVoluntarioAction extends ActionSupport{
     private State myState;
     //getters and setters
}

in your jsp add myState like you will usually do

Now, create a file called xwork-conversion.properties. Put this file in your classes directory under WEB-INF. This class will be responsible for converting your state string to State Object

Next, create a converter class. this class will be responsible for doing the conversion.

public abstract class MyStrutsTypeConverter extends StrutsTypeConverter
{
    public Object convertFromString(Map map, String strings[], Class type)
    {
        String value = strings[0];
        if(value != null && value.equals("state_string_from_jsp"))
        {
          //Use your dao to retrieve your state object and return the state object
        }
        return null;
    }

public String convertToString(Map map, Object o)
{
        if(o instanceof State)
        {
            State data = (State)o;
            //return the  state string
        }
        return "";
}
}

Once you are done with this. The next thing to do is to edit the previously created xwork-conversion.properties. The format is a key-value i.e. the class_name_to_convert=converter_class

full_package_name_of_your_state_class.State=full_package_name_of_your_converter_class.MyStrutsTypeConverter

After that deploy your application.

Let me know if you have any issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!