美文网首页
消息处理

消息处理

作者: 黎涛note | 来源:发表于2017-12-12 00:03 被阅读0次
• 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";
            }
        }
    }
}

相关文章

  • 处理消息

    真的很不理解某些人处理消息的方式,他们看见了消息却不回,为什么?懒得回吗?那你就不要被我看到你留下的蛛丝马迹。 我...

  • 消息处理

    • Struts2将消息分为三种类型:错误、消息、字段错误。 • 在ActionSupport类中提供了对应的方法...

  • 消息处理

    Android中的异步消息处理主要由四个部分组成,Message、Handler、MessageQueue和Loo...

  • Android 消息机制(源码分析)

    Message 数据载体 Handler 发送消息、处理消息、移除消息 发送消息 移除消息 处理消息 Messag...

  • 开发中使用RabbitMQ的手动确认机制

    使用消息队列处理消息的时候,我们可能会遇到以下问题: 消息处理失败 消息体本身有误 消息重复处理 消息丢失 对于消...

  • handler+Message+MessageQueue+Loo

    1.handler消息处理器,负责处理消息 2.Message消息,包含消息id、被处理的对象 3.Message...

  • 异步消息处理

    异步消息处理 android耗时操作只能放在非主线程,message从子线程进入主线程,从不能更新UI到更新UI,...

  • Android 消息处理

    其实对于初学者来说Handler的使用很容易会很容易上手,但是对于其中的机制的理解,却不会那么简单,甚至1,2年经...

  • MFC发送自定义消息-PostMessage和SendMessa

    PostMessage:把消息投放到线程的消息队列,不能消息被处理就立即返回 SendMessage:消息被处理完...

  • 使用消息转发处理消息崩溃

    在移动端APP开发中,崩溃率是一个衡量APP性能的一个很重要的指标。常见的崩溃有服务器返回数据崩溃、内存泄漏崩溃、...

网友评论

      本文标题:消息处理

      本文链接:https://www.haomeiwen.com/subject/vxfgixtx.html