Struts2 actionError and actionMessage.







Download code and jars : Link.

File : index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index page</title>
</head>
<body>
<center>
<s:if test="hasActionErrors()">
<div>
<span style="text-align: center; color: red; font-size: 17px;"><s:actionerror /></span>
</div>
</s:if>
<s:if test="hasActionMessages()">
<div>
<span style="text-align: center; color: green; font-size: 17px;"><s:actionmessage /></span>
</div>
</s:if>
<br>
<form action="checkName" method="post">
<input type="text" name="firstName">
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>



File : struts.xml

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

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

<struts>
<constant name="struts.devMode" value="true" />
<include file="struts-default.xml"/>
<package name="actionErrorActionMessage" extends="struts-default">
  <action name="checkName" class="com.codeFactory.checkNameAction">
  <result name="success">index.jsp</result>
  </action>
</package>
</struts>



File : 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_2_5.xsd"
 id="WebApp_ID" version="2.5">

  <display-name>actionErrorActionMessage</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

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

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



File : checkNameAction.java

package com.codeFactory;

import com.opensymphony.xwork2.ActionSupport;

public class checkNameAction extends ActionSupport{
private String firstName;
public String execute()
{
if(getFirstName().equals("parth"))
{
addActionMessage("Welcome admin.");
}
else
{
addActionError("Wrong input.");
}
return SUCCESS;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}

Comments

Popular posts from this blog

How to get IP address and MAC address of client in java | #CodeFactory

At Least One Checkbox Is Checked From All CheckBox Group | #CodeFactory

FTP Connection In Installed Alfresco | Code Factory