• Struts2将消息分为三种类型:错误、消息、字段错误。
• 在ActionSupport类中提供了对应的方法:
addActionError(String anErrorMessage)
addActionMessage(String aMessage)
addFieldError(String fieldName, String errorMessage)、
• 在jsp页面中使用对应的标签即可显示消息
<s:actionerror/>
<s:actionmessage/>
<s:fielderror></s:fielderror>
【示例】
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<script>
function login()
{
<span style="color:#000000;">form.action="userLoginRegist!regist";</span>
//login同理
}
</script>
<style type="text/css">
.errorMessage{
color:red;
display:inline;
padding:0px;
margin:0px;
}
.errorMessage li{
display:inline;
}
.actionMessage{
color:bule;
}
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method ="post" action="login.action">
账号:<input type="text" name="userInfo.account" /><s:fielderror fieldName="userInfo.account"></s:fielderror><br/>
密码:<input type="password" name="userInfo.pwd" /><s:fielderror fieldName="userInfo.pwd"></s:fielderror><br/>
<input type="submit" value="登录">
<a href = "register.jsp">注册</a>
<s:actionerror/>
</form>
</body>
</html>
LoginAction.java
package com.xixi.struts_demo.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xixi.struts_demo.dto.UserInfo;
import com.xixi.struts_demo.service.UserService;
public class LoginAction extends ActionSupport {
/*
*/
private static final long serialVersionUID = 1L;
/*
* 创建Service层实例对象
*/
private UserService userService = new UserService();
/*
* 创建客户端参数示例对象UserInfo
*/
private UserInfo userInfo = null;
public UserInfo getUserInfo() {
return userInfo;
}
public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}
/**
* 服务器端输入校验
*/
@Override
public void validate() {
if (this.userInfo == null) {
super.addActionError("请保证参数的提交!");
} else {
//判断账号输入
if (this.userInfo.getAccount() == null ) {
//添加字段错误
super.addFieldError("userInfo.account","用户名不能为空!");
} else if (this.userInfo.getAccount().length()<6) {
//添加字段错误
super.addFieldError("userInfo.account","用户名长度不足六个字母,请重新输入!");
}
//判断密码输入
if (this.userInfo.getPwd() == null ) {
//添加字段错误
super.addFieldError("userInfo.pwd","请输入密码!");
} else if (this.userInfo.getPwd().length()<6) {
//添加字段错误
super.addFieldError("userInfo.pwd","密码长度不足六个字母,请重新输入!");
}
}
}
/*
* 在执行execute()方法之前,客户端的参数会自动检测LoginAction类中的set方法,将客户端提交的参数提交到类中各自的属性中
* 如果传递过来的参数,用于接收的参数不是String类型,structs会自动转化
*/
/**
* struts框架自动调用action的execute()方法
*/
public String execute(){
System.out.println("account = " + this.userInfo.getAccount() +" , pwd = "+this.userInfo.getPwd());
//判断用户的账号密码是否正确
UserInfo info = this.userService.getInfoByAccount(this.userInfo.getAccount());
if(info==null){
//账号有误
System.out.println("账号有误");
super.addActionError("账号错误");
super.addFieldError("userInfo.account", "你输入的账号有误");
return "login";
}else{
if(info.getPwd().equals(this.userInfo.getPwd())){
//登陆成功
System.out.println("登陆成功");
//将info对象放入值栈中
ActionContext.getContext().getValueStack().set("info", info);
return "success";
}else{
//密码错误
System.out.println("密码错误");
super.addActionError("密码不正确");
super.addFieldError("userInfo.pwd", "你输入的密码有误");
return "login";
}
}
}
}
网友评论