HI, I am trying to code a simple user-id and login webpage in struts. I need to show error message when user-id or password field is left blank.
LoginAction.java: package com.example.action; import com.example.form.*; import org.apache.struts.action.Action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; //import org.apache.struts.action.ActionErrors; //import org.apache.struts.action.ActionMessages; import org.apache.struts.action.ActionForward; public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception { LoginForm form1 = (LoginForm)form; if(form1.getuserName().equals(form1.getpassword())) { return mapping.findForward("success"); } else { return mapping.findForward("failure"); } } } LoginForm.java: package com.example.form; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMessages; import org.apache.struts.action.ActionMessage; import com.example.*; public class LoginForm extends ActionForm { private static final long serialVersionUID = -473562596852452021L; private String userName= null; private String password= null; public void setuserName(String userName) { this.userName=userName; } public String getuserName() { return userName; } public void setpassword(String password) { this.password=password; } public String getpassword() { return password; } public void reset(ActionMapping mapping ,HttpServletRequest request) { this.userName=null; this.password=null; } public ActionErrors validate(ActionMapping mapping ,HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if(userName==null && userName.length()==0) { errors.add("userName", new ActionMessage("error.userName.required")); } if(password==null && password.length()==0) { errors.add("password", new ActionMessage("error.password.required")); } return errors; } } Below is the code that I am using in MessageResources.properties file: error.userName.required = User Name is mandatory error.password.required = Password is mandatory ResourceManager.java: package com.example; import java.util.ResourceBundle; public class ResourceManager { private static ResourceBundle bundle=ResourceBundle.getBundle("com.example.MessageResources"); public static String getString(String key){ return bundle.getString(key); } } Struts-config.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config SYSTEM "struts-config_1_0.dtd" > <struts-config> <form-beans> <form-bean name= "LoginForm" type ="com.example.form.LoginForm"/> </form-beans> <global-exceptions></global-exceptions> <global-forwards></global-forwards> <action-mappings> <action path="/Login" type="com.example.action.LoginAction" name="LoginForm" scope="session" validate="true" input="login.jsp"> <forward name="success" path="/jsp/firstScreenSuccess.jsp"/> <forward name="failure" path="/jsp/firstScreenFailure.jsp"/> </action> </action-mappings> <message-resources parameter="com.example.form.MessageResources"/> </struts-config> Web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>SecondStrutsApplication</display-name> <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>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>jsp/login.jsp</welcome-file> </welcome-file-list> <taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/tlds/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://struts.apache.org/tags-bean</taglib-uri> <taglib-location>/WEB-INF/tlds/struts-bean.tld</taglib-location> </taglib> </web-app> -- View this message in context: http://struts.1045723.n5.nabble.com/Not-able-to-show-error-message-when-user-id-filed-is-empty-tp4379782p4379782.html Sent from the Struts - User mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org