struts2 jsp和action相互传值通信的方式

首先是jsp向action传值:

首先你的jsp前端代码的name属相在action中必须要有set和get方法,要是属性是引用值得话,

在引用类的代码中也要有相应的set和get方法。如:

jsp:<s:textfield name="user.name" label="您的账号"></s:textfield>

action:

 

    private Iuser user;

    private IuserPort u = new IuserDAO();

    private Map session;

 

    public String execute() {

if (u.find_by_name_pass(user)) {//验证用户

   setSession((Map) ActionContext.getContext().getSession());//session作用域

   getSession().put("user", user);

   return SUCCESS;

}

this.addActionMessage("请检查您的用户名和密码");

return INPUT;

    }

 

    /**

     * @return the user

     */

    public Iuser getUser() {

return user;

    }

 

    /**

     * @param user the user to set

     */

    public void setUser(Iuser user) {

this.user = user;

    }

 

    /**

     * @return the u

     */

    public IuserPort getU() {

return u;

    }

 

    /**

     * @param u the u to set

     */

    public void setU(IuserPort u) {

this.u = u;

    }

 

    /**

     * @return the session

     */

    public Map getSession() {

return session;

    }

 

    /**

     * @param session the session to set

     */

    public void setSession(Map session) {

this.session = session;

    }

}

user的bean:
package com.fyz.pojo;
 
/**
 * Iuser entity. @author MyEclipse Persistence Tools
 */
 
public class Iuser implements java.io.Serializable {
 
// Fields
 
private Integer id;
private String name;
private String pass;
private String stat;
 
// Constructors
 
/** default constructor */
public Iuser() {
}
 
public Iuser(String name, String pass){
this.name = name;
this.pass = pass;
}
 
/** full constructor */
public Iuser(String name, String pass, String stat) {
this.name = name;
this.pass = pass;
this.stat = stat;
}
 
// Property accessors
 
public Integer getId() {
return this.id;
}
 
public void setId(Integer id) {
this.id = id;
}
 
public String getName() {
return this.name;
}
 
public void setName(String name) {
this.name = name;
}
 
public String getPass() {
return this.pass;
}
 
public void setPass(String pass) {
this.pass = pass;
}
 
public String getStat() {
return this.stat;
}
 
public void setStat(String stat) {
this.stat = stat;
}
 
}

action向jsp传值:

 

action:

 

    private Iuser user;

    private IuserPort u = new IuserDAO();

    private Map session;

 

    public String execute() {

if (u.find_by_name_pass(user)) {//验证用户

   setSession((Map) ActionContext.getContext().getSession());//session作用域

   getSession().put("user", user);

   return SUCCESS;

}

this.addActionMessage("请检查您的用户名和密码");

return INPUT;

    }

 

    /**

     * @return the user

     */

    public Iuser getUser() {

return user;

    }

 

    /**

     * @param user the user to set

     */

    public void setUser(Iuser user) {

this.user = user;

    }

 

    /**

     * @return the u

     */

    public IuserPort getU() {

return u;

    }

 

    /**

     * @param u the u to set

     */

    public void setU(IuserPort u) {

this.u = u;

    }

 

    /**

     * @return the session

     */

    public Map getSession() {

return session;

    }

 

    /**

     * @param session the session to set

     */

    public void setSession(Map session) {

this.session = session;

    }

}

 

jsp:

 姓名:<s:textfield value="%{#session.user.name}"></s:textfield>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值