简体   繁体   中英

How to make form display data from underlying bean in struts2?

I want data from java bean to be displayed on struts <s:form> on jsp page. So that user can see what is stored in database and be able to edit.

So, here is my action class with User bean.

public class UserAction extends ActionSupport implements ModelDriven<User> {

    @Valid
    private User user = new User();

        @Override
    public User getModel() {
        return user;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

Here is my jsp page that should displayed User's properties when page is loaded:

<s:form action="createEditUser" method="post">
        <s:push value="user" />
        <s:textfield key="name" />
        <s:radio list="{'Male','Female'}" key="gender" />
        <s:select list="{'USA','Ukraine','Urugvay','Uganda'}" key="country" />
        <s:textarea cols="20" rows="10" key="aboutYou" />
        <s:checkbox key="mailingList" />
        <s:submit key="submit" />
    </s:form>

When user clicks on link to access this form the corresponding User object is loaded from db and assigned to UserAction.user variable.

But above form doesn't display properties of User like name,gender. User object is filled with data but form isn't. I thought <s:push value='user' /> will help me but it didn't.

Don't you see what's wrong?

EDIT . Here is code to fetch user from db:

public String getUserById() {
        HttpServletRequest servletrequest = (HttpServletRequest) ActionContext.getContext().get(
                ServletActionContext.HTTP_REQUEST);
        try {
            user = userDao.read(Long.parseLong(servletrequest.getParameter("userId")));
            newUser = false;
            System.out.println("method getUserById()=" + user);
        } catch (HibernateException | NumberFormatException e) {
            e.printStackTrace();
            return INPUT;
        }
        return SUCCESS;
    }

User is fetched successfully. It is printed.

User user = new User(); exists for creating new user and saving it into db.

My interceptors in struts.xml :

<interceptors>
            <interceptor-stack name="strutscrudStack">
                <interceptor-ref name="defaultStackHibernate">
                    <param name="exception.logEnabled">true</param>
                    <param name="exception.logLevel">DEBUG</param>
                    <param name="params.excludeParams">^struts\..*</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="strutscrudStack"/>

This is an action that calls getUserById() :

<action name="gotoEditUser" class="com.strutscrud.action.UserAction" method="getUserById">
            <result name="success">/createUpdateUser.jsp</result>
            <result name="input">/allUsers.jsp</result>
        </action>

Thank you!

To make display data of loaded from db User object I have to use ModelDriven interface in my action class and <s:push> tag in my jsp page.

So, my UserAction class hasn't changed but jsp page got <s:push> :

<s:form action="createEditUser" method="post">
        <s:push value="user">
            <s:textfield key="name" />
            <s:radio list="{'Male','Female'}" key="gender" />
            <s:select list="{'USA','Ukraine','Urugvay','Uganda'}" key="country" />
            <s:textarea cols="20" rows="10" key="aboutYou" />
            <s:checkbox key="mailingList" />
            <s:submit key="submit" />
        </s:push>
    </s:form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM