属性驱动不常用。
action类,提供成员变量的set方法
public class RootloginAction extends ActionSupport {
String name;
String password;
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println(name);
System.out.println(password);
System.out.println("成功");
return SUCCESS;
}
}
属性驱动
原类
public class Rooter {
int id;
String name;
String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
action类
package com.action;
import com.opensymphony.xwork2.ActionSupport;
import com.rooter.Rooter;
public class RootloginAction extends ActionSupport {
private Rooter rooter;
public Rooter getRooter() {
return rooter;
}
public void setRooter(Rooter rooter) {
this.rooter = rooter;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println(rooter.getName());
System.out.println(rooter.getPassword());
System.out.println("成功");
return SUCCESS;
}
}
最重要的一点是必须在jsp中,提供对象的属性值
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="SSH_Conducture/Index.action" method="post" style="color: #008040">
管理员登录
<br>账号<br><input type="text" name="rooter.name"><br>密码<br>
<input type="text" name="rooter.password"><br>
<input type="submit">
</form>
</body>
</html>