Property-Driven
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index" class="action.UserAction">
<result name="success">success.jsp</result>
</action>
</package>
</struts>
UserAction
package action;
import com.opensymphony.xwork2.Action;
public class UserAction implements Action{
private String username;
private String password;
public UserAction() {
super();
}
private UserAction(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
System.out.println("初始化成功!");
return "success";
}
public String test(){
System.out.println(username+"====="+password);
return "success";
}
}
index.jsp
<form action="demo!test" method="get">
用户名:
<input name="username" />
密码:
<input name="password" />
<input type="submit" value="提交" />
</form><!--如果页面是user.username,user.password那么只要在action里面放一个user和user的set、get方法就够了,user被自动填满。-->
访问index后,在struts.xml中查找,后来数据在UserAction里输出,然后跳到success.jsp页面(网页上就说成功了,so在此省略)。