struts2学习笔记2----参数传递,一个Action类中有多个方法时的指定调用

1.若传输路径上带参数,则在action中定义一个属性名为参数名的属性,且加上get,set方法,如:

    <a href="<%=path%>/hello.action?username=TOM">跳转欢迎界面</a><br>

则在Action类中有:

private String username;和其对应的get,set方法就可以在action中获取传过来的参数.

2.传输的是多个参数且为对象中的值,这里用form提交

   <form action="<%=path %>/hello.action">
    用户名:<input type="text" name="user.username"><br>
    密码:<input type="password" name="user.passwd"><br>
    <input type="submit" value="提交">

    </form>

在Action中有private User user;属性和其对应的get,set方法(这里的User为DTO)如:

public class WelcomeAction {

private User user;

public String execute(){
System.out.println(user.getUsername()+","+user.getPasswd());
return "success";
}

public User getUser() {
return user;
}

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

}

3.一个Action类中有多个方法时的指定调用

action中的配置:

        <action name="time" class="com.pp.struts2.action.TimeAction">
<result name="success" type="dispatcher">
/jsps/time.jsp
</result> //result可以有多个,分别跳转到不同的页面,name值与Action类中的方法的返回值对应

        </action>


action类中有多个方法:

   public class TimeAction extends ActionSupport {

public String execute() {

Date date = new Date();
String nowStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

ServletActionContext.getRequest().setAttribute("nowStr", nowStr);//获取request且设值
return SUCCESS;
}

public String time2() {

Date date = new Date();
String nowStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(date);

ServletActionContext.getRequest().setAttribute("nowStr", nowStr);
return SUCCESS;
}
}

<a href="<%=path%>/time.action">显示服务器时间</a><br>//默认指向execute方法(好吧,他没说,我猜的..)
<a href="<%=path%>/time!time2.action?">显示服务器时间2</a> //指定方法的格式:!方法名.action,time指向的是TimeAction类.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值