JMohasin
JMohasin

Reputation: 533

How to prepopulate form in struts2 from database values

I want to show an edit form where user can change its old data. So i want to prepoulate form with values picked up from databaase?

Please tell me how to do this in struts2?

Upvotes: 0

Views: 3827

Answers (1)

MohanaRao SV
MohanaRao SV

Reputation: 1125

This interceptor calls prepare() on actions which implement Preparable. This interceptor is very useful for any situation where you need to ensure some logic runs before the actual execute method runs.

PreparableInterceptor

Example : Struts2 UI Tags similar to pre-population logic.

package com.examples;
public class RegistrationAction extends ActionSupport implements Preparable {

    @Override
    public void prepare() throws Exception {
        // get the data that you want to pre-populate
    }

    public String execute() {
        // you action logic
        return SUCCESS;
    }

}



<!-- Calls the params interceptor twice, allowing you to pre-load data for the second time parameters are set -->
<!-- don't forgot to add prepare interceptor to interceptor stack -->
 <action name="someAction" class="com.examples.RegistrationAction">
     <interceptor-ref name="params"/>
     <interceptor-ref name="prepare"/>
     <interceptor-ref name="basicStack"/>
     <result name="success">good_result.ftl</result>
 </action>

Upvotes: 2

Related Questions