Action Message and Action Error in Struts2

Find below code to check how action Error and Action Message is working in Struts2

####login.jsp


<%--
    Document   : login
    Created on : Oct 09, 2016, 2:50:34 PM
    Author     : shiboo
--%>
<!DOCTYPE html>
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Login</title>
</head>
<body>
<center><h3>ActionMessage Example</h3>
<s:actionerror />
<s:form action="login">
<s:textfield name="userName"  label="Username" />
<s:password name="password"  label="password" />
<s:submit value="Login" />
</s:form>
</center>

</body>

</html>


####success.jsp

<%--
    Document   : success
    Created on : Oct 09, 2016, 2:41:34 PM
    Author     : shiboo
--%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h3>ActionError &amp; ActionMessage Examples</h3>
<s:actionmessage />
</body>
</html>

###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>
<package name="default" extends="struts-default">
<action name="login" class="com.Messageaction.LoginAction">
<result name="success">success.jsp</result>
<result name="input">login.jsp</result>
</action>
</package>
</struts>


###LoginAction.java

package com.Messageaction;
import com.opensymphony.xwork2.ActionSupport;

/**
 *
 * @author shiboo
 *
 */
public class LoginAction extends ActionSupport {

private static final long serialVersionUID = 6677091252031583948L;
private String userName;
private String password;


public String execute() {

return SUCCESS;
}

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;
}

public void validate() {

if (userName.isEmpty()) {
addActionError("Username is not blanked");
} else {
if(password.isEmpty()){
addActionError("Password is not blanked");
}else{
addActionMessage("Welcome " + userName);
}}
}
}


#### 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>Struts2</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>


Please find below screenshot 







Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Online Book Store Website Using Java

How to read CSV File in Java