一、请求参数直接注入对应Action的属性中
要求:
(1)Action类必须为要接受参数的成员变量提供setXXX()方法;
(2)jsp中表单的name属性须与Action中接受参数的成员变量的setter方法对应,遵循JavaBean命名规则。
login1.jsp
<form action="${pageContext.request.contextPath}/login1.action">
<h1>struts登录测试</h1>
<fieldset style="width: 300px; padding-top: 20px">
<span style="color:red;"><s:fielderror></s:fielderror></span>
<legend><strong>用户登录</strong></legend>
<div>
<label for="username">用户名:</label>
<input type="text" id="username" name="username"> <br> <br>
<label for="password">密 码:</label>
<input type="text" id="password" name="password"> <br> <br>
<input type="submit" value="登 录"
style="width: 120px;height:25px;;margin-left: 70px;background: green;">
</div>
</fieldset>
</form>
struts.xml
<package name="default" namespace="/" extends="struts-default">
<action name="login1" class="cn.xiyou.action.Login1Action">
<!-- name为逻辑视图名 -->
<result name="success">/WEB-INF/jsp/login1_success.jsp</result>
<result name="input" type="dispatcher">login1.jsp</result>
</action>
</package>
Login1Action.java
package cn.xiyou.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class Login1Action extends ActionSupport {
private static final long serialVersionUID = -6730211106850626711L;
private String username;
private String password;
@Override
public String execute() throws Exception {
if("admin".equals(username) && "1234".equals(password)) {
System.out.println("用户名和密码正确");
return Action.SUCCESS;
} else {
System.out.println("用户名和密码错误");
return Action.ERROR;
}
}
/**
* 对传来的数据进行检验
*/
@Override
public void validate() {
if(!"admin".equals(username)) {
addFieldError("username_error", "用户名错误");
System.out.println("用户名错误");
return;
}
if(!"1234".equals(password)) {
addFieldError("password_error", "登录密码错误");
System.out.println("登录密码错误");
}
}
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;
}
}
login1_success.jsp
<h1><span style="color: green;">${username }</span></h1> 登录成功!
二、域模型(DomainModel)注入,将参数注入到对应的JavaBean的属性中
要求:
(1)Action类创建接受参数的实体变量,并提供setXXX()方法;
(2)jsp中表单的name属性写法要求:Action类中Bean的成员变量名.Bean的属性名。如:<
input type=”text” id=”username” name=”user.username”>
login2.jsp
<form action="${pageContext.request.contextPath}/login1.action">
<h1>struts登录测试</h1>
<fieldset style="width: 300px; padding-top: 20px">
<span style="color:red;"><s:fielderror></s:fielderror></span>
<legend><strong>用户登录</strong></legend>
<div>
<label for="username">用户名:</label>
<input type="text" id="username" name="user.username"> <br> <br>
<label for="password">密 码:</label>
<input type="text" id="password" name="user.password"> <br> <br>
<input type="submit" value="登 录"
style="width: 120px;height:25px;;margin-left: 70px;background: green;">
</div>
</fieldset>
</form>
struts.xml
<package name="default" namespace="/" extends="struts-default">
<action name="login2" class="cn.xiyou.action.Login2Action">
<!-- name为逻辑视图名 -->
<result name="success">/WEB-INF/jsp/login2_success.jsp</result>
<result name="input" type="dispatcher">login2.jsp</result>
</action>
</package>
Login2Action.java
package cn.xiyou.action;
import cn.xiyou.entity.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class Login2Action extends ActionSupport {
private static final long serialVersionUID = -6730211106850626711L;
private User user;
@Override
public String execute() throws Exception {
if("admin".equals(user.getUsername()) && "1234".equals(user.getPassword())) {
System.out.println("用户名和密码正确");
return Action.SUCCESS;
} else {
System.out.println("用户名和密码错误");
return Action.ERROR;
}
}
/**
* 对传来的数据进行检验
*/
@Override
public void validate() {
if(!"admin".equals(user.getUsername())) {
addFieldError("username_error", "用户名错误");
System.out.println("用户名错误");
return;
}
if(!"1234".equals(user.getPassword())) {
addFieldError("password_error", "登录密码错误");
System.out.println("登录密码错误");
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
login2_success.jsp
<h1><span style="color: green;">${user.username }</span></h1> 登录成功!
三、模型驱动(ModelDriven<
T>
)注入,将参数注入到对应的JavaBean的属性中
要求:
(1)Action类创建接受参数的实体变量,手动实例化,并提供setXXX()方法;
(2)重写T getModel();方法,返回(1)中的实体。
login3.jsp
<form action="${pageContext.request.contextPath}/login1.action">
<h1>struts登录测试</h1>
<fieldset style="width: 300px; padding-top: 20px">
<span style="color:red;"><s:fielderror></s:fielderror></span>
<legend><strong>用户登录</strong></legend>
<div>
<label for="username">用户名:</label>
<input type="text" id="username" name="username"> <br> <br>
<label for="password">密 码:</label>
<input type="text" id="password" name="password"> <br> <br>
<input type="submit" value="登 录"
style="width: 120px;height:25px;;margin-left: 70px;background: green;">
</div>
</fieldset>
</form>
struts.xml
<package name="default" namespace="/" extends="struts-default">
<action name="login3" class="cn.xiyou.action.Login3Action">
<!-- name为逻辑视图名 -->
<result name="success">/WEB-INF/jsp/login3_success.jsp</result>
<result name="input" type="dispatcher">login3.jsp</result>
</action>
</package>
Login3Action.java
package cn.xiyou.action;
import cn.xiyou.entity.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class Login3Action extends ActionSupport implements ModelDriven<User> {
private static final long serialVersionUID = -6730211106850626711L;
private User user = new User();//一定要手动实例化(除非前台<input>标签的name属性写法为<input name="user.username">)
@Override
public User getModel() {
return this.user;
}
@Override
public String execute() throws Exception {
if("admin".equals(user.getUsername()) && "1234".equals(user.getPassword())) {
System.out.println("用户名和密码正确");
return Action.SUCCESS;
} else {
System.out.println("用户名和密码错误");
return Action.ERROR;
}
}
/**
* 对传来的数据进行检验
*/
@Override
public void validate() {
if(!"admin".equals(user.getUsername())) {
addFieldError("username_error", "用户名错误");
System.out.println("用户名错误");
return;
}
if(!"1234".equals(user.getPassword())) {
addFieldError("password_error", "登录密码错误");
System.out.println("登录密码错误");
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
login3_success.jsp
<h1><span style="color: green;">${user.username }</span></h1> 登录成功!
四、公共实体类
package cn.xiyou.action;
import cn.xiyou.entity.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class Login3Action extends ActionSupport implements ModelDriven<User> {
private static final long serialVersionUID = -6730211106850626711L;
private User user = new User();//一定要手动实例化(除非前台<input>标签的name属性写法为<input name="user.username">)
@Override
public User getModel() {
return this.user;
}
@Override
public String execute() throws Exception {
if("admin".equals(user.getUsername()) && "1234".equals(user.getPassword())) {
System.out.println("用户名和密码正确");
return Action.SUCCESS;
} else {
System.out.println("用户名和密码错误");
return Action.ERROR;
}
}
/**
* 对传来的数据进行检验
*/
@Override
public void validate() {
if(!"admin".equals(user.getUsername())) {
addFieldError("username_error", "用户名错误");
System.out.println("用户名错误");
return;
}
if(!"1234".equals(user.getPassword())) {
addFieldError("password_error", "登录密码错误");
System.out.println("登录密码错误");
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}