Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem in displaying the error strings.

Status
Not open for further replies.

techno012

Programmer
Feb 15, 2005
1
US
Jsp is not showing the error messages, even though they have been added.. I am a beginner in struts..

My App is a simple one. User enters userid, password. It goes to main.jsp from login.jsp. To simulate errors, I just added an error in Action class. In case of error, it should be showing up login.jsp instaed of main.jsp. This is working fine. Unfortunately, it is not showing the errors that have been passed. Please give some clues.
---------------------------------------------------------
Login.jsp:

<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet"
type="text/css">
<TITLE>Login.jsp</TITLE>
</HEAD>
<BODY>
<html:form action="/login" name="loginForm" type="com.scif.webstruts.forms.LoginForm" >
<html:errors />
<html:text property="userid" /> <html:errors property="userid" /><br>
<html:text property="password" /> <html:errors property="password" /><br>

<html:submit /><html:cancel /><html:reset />

</html:form>

</BODY>
</HTML>
----------------------------------------------------

Action Class's execute method:

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("In execute() of LoginAction1");
ActionErrors errors = new ActionErrors();
System.out.println("In execute() of LoginAction2");
ActionForward forward = new ActionForward();
System.out.println(" In execute() of LoginAction3");
LoginForm loginForm = (LoginForm) form;

ActionMessages messages = new ActionMessages();
ActionMessage msg;
msg = new ActionMessage("error.userid.required");//, "Hellolll");
messages.add("msg1", msg);
saveMessages(request, messages);
//request.setAttribute("loginForm", loginForm);

try {

// do something here

} catch (Exception e) {

// Report the error using the appropriate name and ID.
errors.add("name", new ActionError("id"));

}

// Forcing it to have some errors....
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError(ActionErrors.GLOBAL_ERROR, "Helloooo" ));
errors.add("password", new ActionError("error.password.required"));
// errors.add("password", new ActionError("error.password.required"));
// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.

if (!errors.empty()) {
System.out.println("Errors exist. Forward to failure page.");
saveErrors(request, errors);
//request.g
forward = mapping.findForward("failure");
}
// Write logic determining how the user should be forwarded.
else forward = mapping.findForward("success");
//forward = mapping.findForward("failure");

// Finish with
return (forward);

}
---------------------------------------------------

ActionMapping:

<action-mappings>
<action name="loginForm" path="/login" scope="session" type="com.scoo.webstruts.actions.LoginAction" input="./Login.jsp" validate="true" >
<forward name="success" path="./Main.jsp">
</forward>
<forward name="failure" path="./Login.jsp">
</forward>
</action>
</action-mappings>
-------------------------------------------------------

<!-- Message Resources -->
<message-resources parameter="com.scoo.webstruts.resources.ApplicationResources"/>

-----------------------------------------------------

Ho do I verify that app is able to take the strings from Resource files?

I am trying to test under WSAD Testing env and tried to do it on Tomcat too.

Thanks for any help.
--------------------------------------------
 
Try adding these to the top of your jsp page:

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

And these lines to your web.xml file, not all are needed, just the ones you need to access, I use the same web.xml file over and over again usually so I just add them all and that allows me to forget about it:

<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

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

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

<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>


Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top