Struts2的ActionError&ActionMessage示例

演示使用Struts2的 ActionError 和 ActionMessage 类

源代码下载

1. ActionError – 是用来发送错误信息反馈给用户 - 通过 <s:actionerror/> 来显示。

<s:if test="hasActionErrors()">
        <div class="error">
            <!--ActionError 是用来发送错误信息反馈给用户-->
            <s:actionerror/>
        </div>
</s:if>

2. ActionMessage – 用于发送信息的反馈消息给用户,通过 <s:actionmessage/> 来显示。

<s:if test="hasActionMessages()">
    <div class="welcome">
        <!--ActionMessage 用于发送信息的反馈消息给用户-->
        <s:actionmessage/>
    </div>
</s:if>

一. 文件夹结构


二. 属性文件

global.properties

问题一:IDEA创建properties示例

问题二:*.properties中文乱码的修正

#Global message
global.username = 账号
global.password = 密码
global.submit = 提交

#Welcome messages
welcome.hello = 您好

#error message
username.required = 请输入账号
password.required = 请输入密码

三. 动作-Action

package com.ray.action;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Created by Ray on 2018/3/21 0021.
 * Struts 2动作不强迫你实现任何接口或扩展类,
 * 它只是需要你实现一个 execute()方法
 * 返回一个字符串来表示其应该返回的结果页面。
 * 扩展ActionSupport类,它适合在大多数情况下。
 **/

public class UserAction extends ActionSupport {

    private String username;
    private String 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 {
        return super.execute();
    }

    @Override
    public void validate() {
        //检查账号是否为空
        if("".equals(getUsername())){
            addFieldError("username",getText("username.required"));
        }
        //检查密码是否为空
        if("".equals(getPassword())){
            addFieldError("password",getText("password.required"));
        }
        //如果用户名等于"Ray"
        if("Ray".equals(getUsername())){
            //显示成功信息(addActionMessage)
            addActionMessage("Welcome");
        }else{
            //显示错误消息(addActionError)
            addActionError("Error");
        }
    }
}

一个经典的动作类,做一个简单的检查,以确认用户名是否等于“Ray",并使用 addActionError()设置错误信息或addActionMessage()设置成功的消息。


四. JSP页面视图

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">

    <title>Struts2 ActionError & ActionMessage 示例</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
    <style type="text/css">
        .error{
            background-color: cornflowerblue;
            border: 1px solid #CCCC00;
            width: 400px;
            margin-bottom: 8px;
        }
        .error li{
            list-style: none;
        }
    </style>

</head>

<body>
<h2>Struts2 ActionError & ActionMessage 示例</h2>
    <s:if test="hasActionErrors()">
        <div class="error">
            <!--ActionError 是用来发送错误信息反馈给用户-->
            <s:actionerror/>
        </div>
    </s:if>
    <s:form action="Login" method="POST">
        <s:textfield key="global.username" name="username"/>
        <s:password key="global.password" name="password"/>
        <s:submit key="global.submit" name="submit"/>
    </s:form>
</body>
</html>

welcome.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">

    <title>My JSP 'MyJsp.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
    <style type="text/css">
        .welcome {
            background-color: #DDFFDD;
            border: 1px solid #009900;
            width: 200px;
        }

        .welcome li {
            list-style: none;
        }
    </style>
</head>

<body>
<h2>Struts 2 ActionError & ActionMessage示例</h2>
<s:if test="hasActionMessages()">
    <div class="welcome">
        <!--ActionMessage 用于发送信息的反馈消息给用户-->
        <s:actionmessage/>
    </div>
</s:if>
<h4>
    <s:property value="getText('welcome.hello')"/>
    <s:property value="username"/>
</h4>
</body>
</html>


五. struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <!--修改扩展名为html-->
    <!--<constant name="struts.action.extension" value="html"/>-->

    <constant name="struts.custom.i18n.resources" value="global"/>

    <package name="struts2" extends="struts-default">
        <action name="Login" class="com.ray.action.UserAction">
            <result name="success">/welcome.jsp</result>
            <result name="input">/login.jsp</result>
        </action>
    </package>

    <!--开发者模式默认false-->
    <!--<constant name="struts.devMode" value="false"/>-->
</struts>


六. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

  <display-name>Archetype Created Web Application</display-name>

  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
      org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>


七. 运行并测试





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Remember_Ray

何其有幸,得你青睐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值