【Struts2复习知识点七】-用Action的属性接收参数

struts.xml

View Code
<struts>
<constant name="struts.devMode" value="true" />
<package name="user" extends="struts-default" namespace="/user">

<action name="user" class="com.bjsxt.struts2.user.action.UserAction">
<result>/user_add_success.jsp</result>
</action>
</package>
</struts>

 UserAction.java

View Code
package com.bjsxt.struts2.user.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

private String name;
private int age;

public String add() {
System.out.println("name=" + name);
System.out.println("age=" + age);
return SUCCESS;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}


访问:http://localhost:8080/project/user/user!add?name=a&age=8

 

name和age会自动注入到 UserAction.java 中。 此注入是通过set方法注入的,换言之setName对应参数name  ,注意拼写及大小写。

posted @ 2012-02-22 11:14  濤叔  阅读(321)  评论(0编辑  收藏  举报