You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Filippo De Luca <dl...@filosganga.it> on 2007/07/04 15:15:35 UTC

[struts 2] Problem in param setting

Hello Struts users,
I am new for the struts 2 and i have some problem with param setting. I
develop a simple action to view user detail: UserViewAction this action have
only a execute method, loading a user by userId param. So the action work
fine, but the jsp page do not display anything.

Follow my code:

package it.filosganga.sic.business;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.*;

import org.apache.commons.lang.builder.*;
import
org.springmodules.validation.bean.conf.loader.annotation.handler.NotBlank;
import
org.springmodules.validation.bean.conf.loader.annotation.handler.NotNull;

/**
 * Classe che rappresenta un utente del sistema
 *
 * @author Filippo De Luca
 * @version 0.2.00, 1/05/2006
 */
public class User implements Serializable {

    /** serial */
    private static final long serialVersionUID = 200L;

    private Long id;

    private Long version;

    @NotNull
    @NotBlank
    private String username;

    @NotNull
    @NotBlank
    private String password;

    @NotNull
    private boolean enabled;

    @NotNull
    private Date expire;

    @NotNull
    private Date credentialsExpire;

    private Set<Role> authorities = new HashSet<Role>();

    private final PropertyChangeSupport pcs = new
PropertyChangeSupport(this);

    public User() {
        /* Empty */
    }

    ...
}

***

public class UserViewAction {
    /** serial */
    private static final long serialVersionUID = 1L;

    private static final Log log = LogFactory.getLog(UserViewAction.class);

    private UserService userService;

    private Long userId;
    private User user;

    /**
     * @return the userService
     */
    public UserService getUserService() {
        return userService;
    }

    /**
     * @param userService
     *            the userService to set
     */
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    /**
     * @return the id
     */
    public Long getUserId() {
        return userId;
    }

    /**
     * @param id
     *            the id to set
     */
    public void setUserId(Long id) {
        this.userId = id;
        log.debug("Setted id = " + id);
    }

    public String execute() {
        user = userService.findUserById(userId);

        log.debug("Loaded User = " + user);

        return "success";
    }

}

***

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>User - view</title>
</head>

<body>
<s:debug/>
User:<br/>
Username:<s:property value="user.username" /><br/>
<br/>
Roles:<br/>
<s:iterator value="user.authorities">
- <s:property value="name"/><br/>
</s:iterator>

</body>
</html>

***

    <package name="user" extends="struts-default" namespace="/User">

        <default-interceptor-ref name="defaultStack"/>

        <action name="list" method="execute" class="
it.filosganga.sic.web.actions.UserListAction">
            <result>/User/list.jsp</result>
        </action>

        <action name="view" method="execute" class="
it.filosganga.sic.web.actions.UserViewAction">
            <result>/User/view.jsp</result>
            <interceptor-ref name="paramsPrepareParamsStack"/>
        </action>

Some code are omitted for easy read. Can anyone help me?

in the past i develop a PreparableAction loading user in prepare method, but
i need to use paramsPrepareParamsStack to run it. What is the best way? load
user in execute method or in prepare method?

Best regards

-- 
Filippo De Luca
dl.filippo@filosganga.it
http://www.filosganga.it
--------------------------------------------------
Circolo Canottieri Roma
Lungotevere Flaminio, 39 - 00196 - Roma - Italia
http://www.canottieriroma.com
--------------------------------------------------

Re: [struts 2] Problem in param setting

Posted by Filippo De Luca <dl...@filosganga.it>.
2007/7/4, Filippo De Luca <dl...@filosganga.it>:
>
> Hello Struts users,
> I am new for the struts 2 and i have some problem with param setting. I
> develop a simple action to view user detail: UserViewAction this action have
> only a execute method, loading a user by userId param. So the action work
> fine, but the jsp page do not display anything.
>
> Follow my code:
>
> package it.filosganga.sic.business;
>
> import java.beans.PropertyChangeListener;
> import java.beans.PropertyChangeSupport;
> import java.io.Serializable;
> import java.util.*;
>
> import org.apache.commons.lang.builder.*;
> import
> org.springmodules.validation.bean.conf.loader.annotation.handler.NotBlank;
> import
> org.springmodules.validation.bean.conf.loader.annotation.handler.NotNull;
>
> /**
>  * Classe che rappresenta un utente del sistema
>  *
>  * @author Filippo De Luca
>  * @version 0.2.00, 1/05/2006
>  */
> public class User implements Serializable {
>
>     /** serial */
>     private static final long serialVersionUID = 200L;
>
>     private Long id;
>
>     private Long version;
>
>     @NotNull
>     @NotBlank
>     private String username;
>
>     @NotNull
>     @NotBlank
>     private String password;
>
>     @NotNull
>     private boolean enabled;
>
>     @NotNull
>     private Date expire;
>
>     @NotNull
>     private Date credentialsExpire;
>
>     private Set<Role> authorities = new HashSet<Role>();
>
>     private final PropertyChangeSupport pcs = new
> PropertyChangeSupport(this);
>
>     public User() {
>         /* Empty */
>     }
>
>     ...
> }
>
> ***
>
> public class UserViewAction {
>     /** serial */
>     private static final long serialVersionUID = 1L;
>
>     private static final Log log = LogFactory.getLog(UserViewAction.class
> );
>
>     private UserService userService;
>
>     private Long userId;
>     private User user;
>
>     /**
>      * @return the userService
>      */
>     public UserService getUserService() {
>         return userService;
>     }
>
>     /**
>      * @param userService
>      *            the userService to set
>      */
>     public void setUserService(UserService userService) {
>         this.userService = userService;
>     }
>
>     /**
>      * @return the id
>      */
>     public Long getUserId() {
>         return userId;
>     }
>
>     /**
>      * @param id
>      *            the id to set
>      */
>     public void setUserId(Long id) {
>         this.userId = id;
>         log.debug("Setted id = " + id);
>     }
>
>     public String execute() {
>         user = userService.findUserById(userId);
>
>         log.debug("Loaded User = " + user);
>
>         return "success";
>     }
>
> }
>
> ***
>
> <%@ page contentType="text/html; charset=UTF-8"%>
> <%@ taglib prefix="s" uri="/struts-tags"%>
> <html>
> <head>
> <title>User - view</title>
> </head>
>
> <body>
> <s:debug/>
> User:<br/>
> Username:<s:property value="user.username" /><br/>
> <br/>
> Roles:<br/>
> <s:iterator value="user.authorities">
> - <s:property value="name"/><br/>
> </s:iterator>
>
> </body>
> </html>
>
> ***
>
>     <package name="user" extends="struts-default" namespace="/User">
>
>         <default-interceptor-ref name="defaultStack"/>
>
>         <action name="list" method="execute" class="
> it.filosganga.sic.web.actions.UserListAction">
>             <result>/User/list.jsp</result>
>         </action>
>
>         <action name="view" method="execute" class="
> it.filosganga.sic.web.actions.UserViewAction">
>             <result>/User/view.jsp</result>
>             <interceptor-ref name="paramsPrepareParamsStack"/>
>         </action>
>
> Some code are omitted for easy read. Can anyone help me?
>
> in the past i develop a PreparableAction loading user in prepare method,
> but i need to use paramsPrepareParamsStack to run it. What is the best way?
> load user in execute method or in prepare method?
>
> Best regards
>
> --
> Filippo De Luca
> dl.filippo@filosganga.it
> http://www.filosganga.it
> --------------------------------------------------
> Circolo Canottieri Roma
> Lungotevere Flaminio, 39 - 00196 - Roma - Italia
> http://www.canottieriroma.com
> --------------------------------------------------


Hello excuse me but this code is ok. But if the UserViewAction implements
ModelDriven so it implement the method:

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


And the jsp page become:

<body>
<s:debug/>
User:<br/>
Username:<s:property value="username" /><br/>
<br/>
Roles:<br/>
<s:iterator value="authorities">
- <s:property value="name"/><br/>
</s:iterator>

</body>

The jsp page don't display anything? why? if the action is a modelDriven
this jsp page have to e correct!

Thanks

-- 
Filippo De Luca
dl.filippo@filosganga.it
http://www.filosganga.it
--------------------------------------------------
Circolo Canottieri Roma
Lungotevere Flaminio, 39 - 00196 - Roma - Italia
http://www.canottieriroma.com
--------------------------------------------------