Cannot bookmark a JSP page - java

I have a JSP page I'm using within the struts framework. When I navigate through my pages of my web app using buttons on a menu bar, it navigates properly and I can access each of the pages (page1.do, page2.do) without any problems. When I bookmark one of the pages (page1.do) and attempt to go to the bookmark, I get an error.
Request[/adminmanagement] does not contain handler parameter named directive
If I manually type in the directive stuff "page1.do?directive=init", I have no problem accessing the page.
Here is my struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="FileUploadForm" type="net.cmpny.cc.forms.FileUploadForm"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward name="init" path="/page1.do?directive=init"/>
</global-forwards>
<action-mappings>
<action path="/adminmanagement"
type="net.cmpny.cc.action.Page1Action"
parameter="directive">
<forward name="admin" path="/WEB-INF/jsp/Page1.jsp" />
</action>
</action-mappings>
</struts-config>
Is there a way of adding the 'directive=init' automatically or forwarding to that page?
I was going to write a filter, but I feel like there's a better/cleaner way of doing it.
Thanks in advance!

A forward won't change the address in the URL bar. You need to do a redirect:
<forward name="init" path="/page1.do?directive=init" redirect="true"/>

Related

Regular Expressions Struts 1.3 Action Mapping

I am working on a project using Struts 1.3 from what I can tell, given that this is at the top of the struts-config-default.xml file:
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
Is there any way to go about mapping a wildcard for an action forward to a jsp file? I have tried all sorts of wildcard variations:
<action path="/hello/candy" type="com.officedepot.globalweb.framework.action.ForwardDisplayAction">
<forward name="success" path="/WEB-INF/jsp/candyStore.jsp" />
</action>
I have a single page application that gets loaded in the 'candyStore.jsp' so i would like all and any URIs after /hello/candy to route to the same JSP. (eg. www.site.com/hello/candy/pageOne, www.site.com/hello/candy/33/jellybean, www.site.com/hello/candy/test all should forward to the jsp candyStore)
Is this at all possible using Struts 1.3 or should I be writing all the possible routes :(
Thanks!
In your action file
public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request , HttpServletResponse response)
throws Exception{
//Perform any code here like error 404.
return mapping.findForward("unspecified");
}
In your struts action path in config file
<forward name="unspecified" path="/candyStore.jsp"/>
About 30 seconds on Google:
https://dzone.com/tutorials/java/struts/struts-example/struts-wildcards-in-action-mapping-example.html
<action-mappings>
<action path="/*Action" type="com.vaannila.reports.{1}Action" name="{1}Form">
<forward name="success" path="/{1}.jsp" />
</action>
</action-mappings>
You would not require the wildcard in the forward's path.

Why I can not get the jsp thought its path?

I use struts in my Java EE project:
In my loading.jsp if I use the below src, I will get 404 error:
<IFRAME src="${pageContext.request.contextPath}/WEB-INF/page/menu/alermDevice.jsp" name="dev" id="dev" frameBorder="0" width="500" scrolling="auto" height="400">
</IFRAME>
But if I use below src:
<IFRAME src="elecMenuAction_alermDevice.do" name="dev" id="dev" frameBorder="0" width="500" scrolling="auto" height="400">
</IFRAME>
I will get the correct information.
This is my struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.ui.theme" value="simple"></constant>
<constant name="struts.action.extension" value="do"></constant>
<package name="system" namespace="/system" extends="struts-default">
<action name="elecTextAction_*" class="elecTextAction" method="{1}">
<result name="save">/system/textAdd.jsp</result>
</action>
<action name="elecMenuAction_*" class="elecMenuAction" method="{1}">
<result name="menuHome">/WEB-INF/page/menu/home.jsp</result>
<result name="title">/WEB-INF/page/menu/title.jsp</result>
<result name="left">/WEB-INF/page/menu/left.jsp</result>
<result name="change">/WEB-INF/page/menu/change.jsp</result>
<result name="loading">/WEB-INF/page/menu/loading.jsp</result>
<result name="logout" type="redirect">index.jsp</result>
<result name="alermStation">/WEB-INF/page/menu/alermStation.jsp</result>
<result name="alermDevice">/WEB-INF/page/menu/alermDevice.jsp</result>
</action>
</package>
</struts>
Why I use the path can not access the JSP? only use the action I can get it yet?
The web server cannot get resources from and below WEB-INF folder. When action is invoked it returns a response as an execution of the result. It's used a result type dispatcher which is used by default to forward request to the specified URL (the requested JSP page).
Dispatcher Result
Includes or forwards to a view (usually a jsp). Behind the scenes
Struts will use a RequestDispatcher, where the target servlet/JSP
receives the same request/response objects as the original
servlet/JSP. Therefore, you can pass data between them using
request.setAttribute() - the Struts action is available.
There are three possible ways the result can be executed:
If we are in the scope of a JSP (a PageContext is available), PageContext's PageContext#include(String) method is
called.
If there is no PageContext and we're not in any sort of include (there is no "javax.servlet.include.servlet_path" in the request
attributes), then a call to
RequestDispatcher#forward(javax.servlet.ServletRequest,
javax.servlet.ServletResponse) is made.
Otherwise, RequestDispatcher#include(javax.servlet.ServletRequest,
javax.servlet.ServletResponse) is called.
When servlet dispatcher is invoked it has not such restriction and can return resource with the same response that was originally requested.

What would be the flow order if I include multiple struts config file in the project

I am using Struts2. Below is my Action Class (TutorialAction).
public class TutorialAction {
public String execute() {
System.out.println("Hello from Execute!");
return "failure";
}
}
I am returning "failure" in execute method of this Action class.
Below are my 2 struts config files :
======================== 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" namespace="/tutorials" extends="struts-default">
<action name="getTutorial" class="com.tushar.action.TutorialAction">
<result name="failure">/ErrorPage.jsp</result>
</action>
</package>
<include file="struts2.xml"></include>
</struts>
In above config file I am including another struts config file(struts2.xml) for same namespace :
======================== struts2.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" namespace="/tutorials" extends="struts-default">
<action name="getTutorial" class="com.tushar.action.TutorialAction">
<result name="failure">/SuccessPage.jsp</result>
</action>
</package>
</struts>
My project is running fine. I am just curious to know if included file in struts.xml (which is struts2.xml) is run after main struts.xml or before ?
Or what would be the output: /SuccessPage.jsp or /ErrorPage.jsp?
Struts configuration is built after xml documents has been parsed on the start up of your application. Then it uses configuration properties to map actions under their namespaces. This mapping is created via iterating all packages which is also a map. If you have the same namespace in other packages then the last will override the previous mapping. You should know that iterating a map doesn't guarantee the order of the retrieved elements. See HashMap.
So, the order in which the namespace mapping is created is not guaranteed and that namespace will only contain those actions put by the iterator at last time. The namespace to actions mapping is used when Struts2 is getting action config from the action mapping (at the time it's creating an action proxy) created after parsing an URL. Then it continues if such action config is found. The results are mapped to an action, and you don't have results with the same name.
Hope it's easy to understand. If you have the same namespace and the same action name, and the same package name which I doubt impossible, such configuration can't be used and might lead to unpredictable results. And this is not important in which order the packages are created. Note the order is important if you have dependency between packages that are absent in your case.
If you have struts2 config like this.
<struts>
<package name="default" namespace="/tutorials" extends="struts-default">
<action name="getTutorial" class="com.tushar.action.TutorialAction">
<result name="failure">/ErrorPage.jsp</result>
</action>
</package>
<include file="struts-module2.xml"></include>
</struts>
or
<struts>
<include file="struts-module1.xml">
<include file="struts-module2.xml"></include>
</struts>
and according to Practical Apache Struts 2 Web 2.0 Projects.
When including files, the order is very important. Dependencies between include files are not automatically determined
and resolved, so if struts-module1.xml is dependent on the
configuration provided in struts-module2.xml (and struts-module2.xml
is configured after struts-module1.xml), an exception thrown. The
solution is to change the file that the dependent configuration is
contained within or to change the order of the include files.
But since you have same url which is /getTutorial the last one you've configured always win because you're overwriting your definitions. So the the first one will be useless, you should give another name if you want to use both.

Struts2 - returning to a specific section and/or part of a .jsp page

I am using the Struts2 framework and trying to get it to return to a specific section id on a page. Currently, it throws an error when I try to do the following:
<struts>
<constant name="struts.devMode" value="false"/>
<package name="default" namespace="/" extends="struts-default">
<action name="default">
<!--suppress Struts2ModelInspection -->
<result type="redirectAction">index.jsp</result>
</action>
<action name="sendEmail" class="com.brickhouse.action.EmailAction">
<result name="success">index.jsp#contact</result>
</action>
<action name="sendNewsletter" class="com.brickhouse.action.NewsletterAction">
<result name="success">index.jsp</result>
</action>
</package>
Of course, if I remove the hashtag and just let the "sendMail" action return to regular index.jsp as opposed to index.jsp#contact it works fine.
The section id that I am trying to return is contact.
<section id="contact">
<div class="container">
. . . .
</div>
</section>
Any thoughts?
You should not use a # sign as the dispatcher result, because dispatcher doesn't change URL the request is forwarded to. The resource with such name couldn't be found. Instead you can use it to build the URL or retuning a redirect result type to skip to the section (the last is never used). For example
<s:url var="skipToContact" value="#contact"/>
<s:a href="%{#skipToContact}">Skip to Contact</s:a>
...
<section id="contact"></section>
will skip to the section when you click on it. Make sure the section is not at the end of the document, i.e. the height from the section to the end of document is greater than a browser window height.

Struts2 Validation Framework doesn't validate Fields

I m new to Struts2. I want to use Validation Framework for a simple 'register' form. Here is my JSP page form elements;
<s:form action="register">
<s:textfield name="name" label="Name" />
<s:textfield name="age" label="Age" />
<s:textfield name="email" label="email"/>
<s:submit value="Register" />
</s:form>
validation error ? = <s:actionerror /><br/>
---Action Message--- <s:actionmessage/>
Here is struts2.xml file;
<!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="register"
class="com.action.RegisterAction">
<result name="success">home.jsp</result>
<result name="input">index.jsp</result>
</action>
</package>
</struts>
RegisterAction class has
String name;
int age;
String email; variables with getters and setters. And its execute() just returns SUCCESS
For these fields validations the following validation xml file is used, for now only one field name is checked. Name of the validation file is RegisterAction-validation.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<!-- Author: Aash -->
<validators>
<field name="name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>name is required.. :) </message>
</field-validator>
</field>
</validators>
Here is the project structure
Please any one let me know how can I come up with this. Thanks in advance.
New Add : According to the struts.xml, if there is an error,(the validation says something is wrong), the page should be redirected to index.jsp ,but when nothing is given for the field 'name', still it goes to home.jsp .
A Solution Found
I added to validation.xml
!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
But it is displayed on the top of Form, I don't know why not printed as actionError. How I get it Printed where I want it to view(example : bottom of the form)?
Since it is a field Error, as you have put elements in the JSP page, the message comes close to the field 'name' . If you wanna get printed it,where u want it to be, <s:fielderror fieldName="name" />
I think the problem is the xml file name. It should be RegisterAction-validation.xml
As this says, the DOCTYPE should be there in the validation.xml.
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
-->
Put parameter validate value make 'true; in in <s:form action="register" validate="true">

Categories