sangeetha
asked on
Struts - Forward page Problem
I have ShowList.jsp ( in my struts ) as follows:
---------------------- -------------------------- ---
| FilterList.jsp | | RegisterMaskList.jsp |
| | | |
---------------------- -------------------------- ---
-------------------------- ---------- ---------- ---------- ----
| |
| show the list here |
| |
| |
-------------------------- ---------- ---------- ---------- ----
As the JSP name describes, each has different operations and the two JSP's are included in ShowList.jsp.
My Action-Mapping in struts-config.xml is as follows:
<action-mappings>
<action attribute="registerMaskFor m" input="/jsp/ManageCapacity /RegisterM askList.js p" name="registerMaskForm" path="/registerMask" scope="request" type="de.grassgmbh.calende rtool.acti on.Registe rMaskActio n" unknown="false" validate="true">
<forward name="r_success" path="/jsp/ManageCapacity/ ShowList.j sp" redirect="false" contextRelative="false" />
</action>
<!-- And similarly for my FilterForm.jsp -->
</action-mappings>
If i submit the /jsp/ManageCapacity/Regist erMaskList .jsp:
1. On *success* of vaidate() method in FormBean , it goes to /jsp/ManageCapacity/ShowLi st.jsp
2. On *failure* of validate() method in my FormBean ( ie. Form submission results in some errors like some field is empty.), it goes to /jsp/registerMask.do and shows the errors.
--> I want BOTH to go to /jsp/ManageCapacity/ShowLi st.jsp. In case of failure(2nd case above), i need to show the errors in /jsp/ManageCapacity/ShowLi st.jsp
How can i resolve this? Any ideas to trace out the problems?
Thanks.
Note: I tried redirect="true" also.
---------------------- --------------------------
| FilterList.jsp | | RegisterMaskList.jsp |
| | | |
---------------------- --------------------------
--------------------------
| |
| show the list here |
| |
| |
--------------------------
As the JSP name describes, each has different operations and the two JSP's are included in ShowList.jsp.
My Action-Mapping in struts-config.xml is as follows:
<action-mappings>
<action attribute="registerMaskFor
<forward name="r_success" path="/jsp/ManageCapacity/
</action>
<!-- And similarly for my FilterForm.jsp -->
</action-mappings>
If i submit the /jsp/ManageCapacity/Regist
1. On *success* of vaidate() method in FormBean , it goes to /jsp/ManageCapacity/ShowLi
2. On *failure* of validate() method in my FormBean ( ie. Form submission results in some errors like some field is empty.), it goes to /jsp/registerMask.do and shows the errors.
--> I want BOTH to go to /jsp/ManageCapacity/ShowLi
How can i resolve this? Any ideas to trace out the problems?
Thanks.
Note: I tried redirect="true" also.
ASKER
Is it possible to include validate() method in Action Class ? ...
Ok..
This is my Action class now: (It re-directs to /jsp/ManageCapacity/ShowLi st.jsp, but it doesnt show any errors ).
public class FilterAction extends Action {
public ActionErrors validate( ActionMapping mapping, HttpServletRequest request) {
ActionErrors actionErrors = new ActionErrors();
if(request.getParameter("s tartDate") == null || request.getParameter("star tDate").tr im().equal s("")) {
actionErrors.add("startDat e", new ActionError("filterForm.st artDate.pr oblem"));
}
if(request.getParameter("e ndDate") == null || request.getParameter("endD ate").trim ().equals( "")) {
actionErrors.add("endDate" , new ActionError("filterForm.en dDate.prob lem"));
}
return actionErrors;
}
public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
FilterForm filterForm = (FilterForm) form;
Logger logger = Logger.getLogger("FilterAc tion");
logger.info("Filter Action Invoked");
return mapping.findForward("succe ss");
}
}
In the Console window, i get "Filter Action Invoked" .
Also, can i use validate() for Struts-Validator using "extends ValidatorForm" in my above Action Class.
Ok..
This is my Action class now: (It re-directs to /jsp/ManageCapacity/ShowLi
public class FilterAction extends Action {
public ActionErrors validate( ActionMapping mapping, HttpServletRequest request) {
ActionErrors actionErrors = new ActionErrors();
if(request.getParameter("s
actionErrors.add("startDat
}
if(request.getParameter("e
actionErrors.add("endDate"
}
return actionErrors;
}
public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
FilterForm filterForm = (FilterForm) form;
Logger logger = Logger.getLogger("FilterAc
logger.info("Filter Action Invoked");
return mapping.findForward("succe
}
}
In the Console window, i get "Filter Action Invoked" .
Also, can i use validate() for Struts-Validator using "extends ValidatorForm" in my above Action Class.
ASKER
i mean if i left the "startDate" field empty, it has to show the errors... but it is not showing. I need to show the error in my ShowList.jsp
Thanks for your patience.
Thanks for your patience.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So basically, you call the validate method in the Action yourself, and if there are any errors, save them into the request object (using the saveErrors method supplied wih the Struts Action class)
Hope this helps :-)
Hope this helps :-)
ASKER
fine...and how can i show the errors in my Page. I see "Got errors" in my console window. Ialso need to show the error "Field cannot be empty" in my output page.
I defnined the error in resources file. and in my FilterList.jsp, i have the the following to show the erros:
<html:messages id="error">
<TR><bean:write name="error"/></TR>
</html:messages>
Thanks for your help. :-)
I defnined the error in resources file. and in my FilterList.jsp, i have the the following to show the erros:
<html:messages id="error">
<TR><bean:write name="error"/></TR>
</html:messages>
Thanks for your help. :-)
try replacing:
<html:messages id="error">
<TR><bean:write name="error"/></TR>
</html:messages>
with
<html:errors/>
And put:
errors.header=
errors.prefix=<TR>
errors.suffix=</TR>
errors.footer=
in your ApplicationResources.prope rties file
Also, make sure redirect="false" in the struts-config.xml
If you redirect, you have to write an interim stage to save the errors into the session instead of the request, and then write a listener to extract them back out again :-)
Tim
<html:messages id="error">
<TR><bean:write name="error"/></TR>
</html:messages>
with
<html:errors/>
And put:
errors.header=
errors.prefix=<TR>
errors.suffix=</TR>
errors.footer=
in your ApplicationResources.prope
Also, make sure redirect="false" in the struts-config.xml
If you redirect, you have to write an interim stage to save the errors into the session instead of the request, and then write a listener to extract them back out again :-)
Tim
ASKER
>> Also, make sure redirect="false" in the struts-config.xml
Ok.. If i have "true", it re-directs but doesn't show errors. If i set to "false" it shows errors (also shows RegisterList JSP, which i want :-) ) but in the URL, i see:
/jsp/filter.do
>> If you redirect, you have to write an interim stage to save the errors into the session instead of the request, and then write a listener to extract them back out again :-)
I think i should go for this. Could you please explain me about this? If this is getting longer, i will post a new Q for this.
Is what i am doing is a good way ? Or, Is there any other way ?
Ok.. If i have "true", it re-directs but doesn't show errors. If i set to "false" it shows errors (also shows RegisterList JSP, which i want :-) ) but in the URL, i see:
/jsp/filter.do
>> If you redirect, you have to write an interim stage to save the errors into the session instead of the request, and then write a listener to extract them back out again :-)
I think i should go for this. Could you please explain me about this? If this is getting longer, i will post a new Q for this.
Is what i am doing is a good way ? Or, Is there any other way ?
ASKER
Ok.. I accept this and post a new Q.
Just small doubts.
1. Is this a good way ?
2. Can i extend ValidatorForm in my Action class, because i want to use Struts-Validator (using validation.xml file) later.
Thanks.
Just small doubts.
1. Is this a good way ?
2. Can i extend ValidatorForm in my Action class, because i want to use Struts-Validator (using validation.xml file) later.
Thanks.
the struts validator will take you back to the page that caused the error.
what you are doing is not what struts expects (going to next page even if there is a validation error), so you have to handle it yourslef like this :-(
You *may* be able to invoke the StrutsValidator yourself...I've never tried :-(
Good luck with it!!
:-)
Tim
what you are doing is not what struts expects (going to next page even if there is a validation error), so you have to handle it yourslef like this :-(
You *may* be able to invoke the StrutsValidator yourself...I've never tried :-(
Good luck with it!!
:-)
Tim
ASKER
Thanks.
https://www.experts-exchange.com/questions/21097945/Struts-Validate-forward-and-re-direct.html
If it is not a problem, just one more Q :
Is there an alternate way of doing instead of my FIRST post. i.e. Instead of having 3 jsp's (2 form-bean, 2 form-action) ?
https://www.experts-exchange.com/questions/21097945/Struts-Validate-forward-and-re-direct.html
If it is not a problem, just one more Q :
Is there an alternate way of doing instead of my FIRST post. i.e. Instead of having 3 jsp's (2 form-bean, 2 form-action) ?
I'm not sure...can you explain the situation a bit more? I am not 100% sure of the problem :-(
Ahhh, you mean stop having an action for each of the 2 jsps?
Sure, you can tie the same action, and actionform to more than one jsp. But you have to be careful if the functionality is different depending on which page you are on, as things can start to get tricky (and debugging can start to become a nightmare) :-(
If I have missed the point, please explain more :-)
Tim
Ahhh, you mean stop having an action for each of the 2 jsps?
Sure, you can tie the same action, and actionform to more than one jsp. But you have to be careful if the functionality is different depending on which page you are on, as things can start to get tricky (and debugging can start to become a nightmare) :-(
If I have missed the point, please explain more :-)
Tim
PS: You should be able to delete your link question in JSP :-)
Then, you can add ActionErrors for faliures in validation, and still forward to the success page...
By default, if the validate() method returns anything, it goes back to the page that was posted, not to another page...
Tim