9:53 am - Tuesday February 28, 2017

Struts logic messagesPresent logic messagesNotPresent example program

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Project Structure: 

 

 

 

 

 

 

 

 

 

 

 

 

 

 Jar Files: 

 

 

 

 

 

 

 

 EmailForm.java: 

package com.candidjava;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class EmailForm extends ActionForm {

	String email;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {

		ActionErrors errors = new ActionErrors();

		errors.add("email.err", new ActionMessage("error.email.required"));

		return errors;

	}

}

LogicMegPresentAction.java:

package com.candidjava;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LogicMegPresentAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// do nothing

		return mapping.findForward("success");
	}

}

Web.xml:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>

    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

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

  </welcome-file-list>

  <taglib>
    <taglib-uri>/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/struts-bean</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/struts-logic</taglib-uri>
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>

  <security-constraint>
    <web-resource-collection>
      <web-resource-name>My secure resources</web-resource-name>
      <description>Resources to be placed under security control.</description>
      <url-pattern>/private/*</url-pattern>
      <url-pattern>/registered/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <role-name>guest</role-name>
    </auth-constraint>
  </security-constraint>

  <login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>WebApp</realm-name>
  </login-config>

  <security-role>
    <description>The role allowed to access our content</description>
    <role-name>guest</role-name>
  </security-role>

</web-app>

Struts-config.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>

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

<struts-config>

	<form-beans>

		<form-bean name="emailForm"
			type="com.candidjava.EmailForm"></form-bean>

	</form-beans>

	<action-mappings>

	 	<action
			path="/LogicTest"
			type="com.candidjava.LogicMegPresentAction"
			name="emailForm"
			validate="true"
			input="/LogicMessagePresent.jsp">

			<forward name="success" path="/LogicMessagePresent.jsp"/>

		</action>

	</action-mappings>

	<message-resources
		parameter="com.candidjava.Email" />

</struts-config>

Welcome.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

 <%@ taglib uri="/struts-html.tld" prefix="html" %>
 <%@ taglib uri="/struts-logic" prefix="logic" %>
  <%@ taglib uri="/struts-bean" prefix="bean" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body bgcolor="pink">

<h2>Struts - &lt;logic:messagesPresent&gt; & &lt;logic:messagesNotPresent&gt;</h2>

<logic:messagesPresent>
	There are errors on this page!
</logic:messagesPresent>
<logic:messagesNotPresent>
	There are no errors on this page!
</logic:messagesNotPresent>

<br/><br/>

<logic:messagesPresent property="email.err">
	Email address has error messages! ERROR_KEY
</logic:messagesPresent>
<logic:messagesNotPresent property="email.err">
	Email address has no error messages! - ERROR_KEY
</logic:messagesNotPresent>

<br/><br/>

<logic:messagesPresent property="email.err" message="true">
	Email address has error messages! - MESSAGE_KEY
</logic:messagesPresent>
<logic:messagesNotPresent property="email.err" message="true">
	Email address has no error messages! - MESSAGE_KEY
</logic:messagesNotPresent>

<br/><br/>

</body>
</html>

LogicMessagePresent.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<!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>Welcome</title>
</head>
<body bgcolor="pink">
<html:form action="/LogicTest">
<p>click
<html:submit>here </html:submit>
to retrieve details using &lt;logic:MessagepresentNotPresent&gt;</p>
</html:form>

</body>
</html>

OUTPUT:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

DOWNLOAD+SOURCECODE


							
							
			
Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply