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类.