开发者

binding paramters in struts2 without the form

HI: When using a form,the parameter form the clien can be bound to an object,for example:

processing-forms.html

In the client:

<s:form action="register">

      <s:textfield name="personBean.firstName" label="First name" />
      <s:textfield  name="personBean.lastName" label="Last name" />
      <s:textfield name="personBean.email"  label ="Email"/>  
      <s:textfield name="personBean.age"  label="Age"  />

      <s:submit/>

</s:form>

In the severside:

public class Register extends ActionSupport {

    private static final long serialVersionUID = 1L;

    private Person personBean;
        //................
}

Then the parameter of the client are bound to the personBean instance.

Now,my problem is how to bind the parameters without a from?

My action work as a service which will be called in the javascript,so how to bind them?

I know how to get the parameters:

Map(String,Object) map=ActionContext.getContext.getParameters();
String firstName= map.get("firstname")[0];

//..........

This is too ugly :(


UPDATE

public class ParaWrapper(){
  private String firstName;
  public void setFirstName(String ..){
    this.firstName=...
  }
  //the getter of firstName
  public ....
}


public MyAction extends ActionSupport{
  private ParaWrapper wrapper;
  public void setXXX()...
  public void getXXX()...
  public void execute(){
    System.out.println(wrapper); //here I can not get the parameters,it seems that the parameters are not poputed to this object.
  }
}

Since I do not use the开发者_如何学JAVA s:form tag,so How do struts know where to put the paramters ?


You handle it the same way. If your field is named firstname, then you will need a setFirstname method on the action. Whether the parameters are coming from a form or from JavaScript is irrelevant.

Update

Based on your revised code example, you will need a getWrapper method on your action to expose the ParaWrapper object.

You can avoid the "wrapper." prefix by implementing the ModelDriven interface and making ParaWrapper your model. Then you would just have parameters such as: firstName, lastName, etc (whatever fields are on ParaWrapper).


I think you shouldn't use private fields for the values that should be set via Struts2.

Explanation:

I don't know how you post the parameters to your action via JavaScript, but it should work if you add the necessary parameters to the URL you call. You can possibly call (as suggested in the mailing list):

http://yourdomain/yourstruts.action?personBean.firstName=a_string&personBean.lastName=my_lastName& ... (more person parameters)

Struts2 will understand the dot-notation and try to set the personBean variable in your target action. If this is of a Bean class (with an empty public constructor and public setters for each parameter), it will generate a new object and call the setters with the parameters. If it cannot access the parameters, nothing can be set.

So, if your setters are public and your PersonBean class is defined correctly, a PersonBean should be in your actions personBean field.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜