Why I can not get the jsp thought its path? - java

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.

Related

Struts namespaces and WEB-INF folder [duplicate]

This question already has an answer here:
Struts2 URL unreachable
(1 answer)
Closed 7 years ago.
I want to put view.jsp file in WEB-INF folder. it lies in WEB-INF\user\view.jsp
<package name="user" extends="struts-default" namespace="user">
<action name="view" class="com.example.user.ViewUserAction">
<result>/WEB-INF/user/view.jsp</result>
</action>
</package>
but Struts changes this url to application/user/WEB-INF/user/view.jsp
so as I understand if my namespace is not "/" I will never visit the JSP in WEB-INF ?
I found where I was wrong.
As Alexander M pointed out in comments no such url will be generated.
The thing is I've tried various bad solutions, but most of them had the next synopsis.
In struts.xml I wrote:
<package name="product" namespace="/" extends="struts-default">
<action name="view" class="com.example.action.product.ViewProduct">
<result name="success">/WEB-INF/view/product/view.jsp</result>
</action>
</package>
and in jsp I refer to somee action as:
View product
but seems like its wrong way to do it - in my case I had urls like:
http://localhost:8080/app/product/product/view.action
which is not valid.
So I changed struts.xml namespace to:
<package name="product" namespace="/product" extends="struts-default">
<action name="view" class="com.example.action.product.ViewProduct">
<result name="success">/WEB-INF/view/product/view.jsp</result>
</action>
</package>
And then in view.jsp used struts <s:a> tag (or you may use <s:url>):
View product
<s:a action="view" namespace="/product"> View product</s:a>
For further reference about packages and namespaces understanding:
Struts Namespace Configuration
Struts Package Configuration

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.

How to return a HTML page from struts.xml itself without going to an action?

If a request is made to a HTML page, how to return it directly from struts.xml without going to an action?
Create an action that has a result in the struts.xml but doesn't have a class, i.e.
<package name="default" extends="struts-default">
<action name="index">
<result name="myPage">/path/to/page.html</result>
</action>
You could also create a global result, that could be found from any action, i.e
<global-results>
<result name="myPage">/path/to/page.html</result>
</global-results>
<action name="index"/>
You could create an interceptor that returns a result while intercepting and action. These are essential elements of the configuration that invoke a dispatcher to forward to requested page.

struts2 web application not found

I have created a struts 2 application and deploy it in tomcat by creating a war file. There is nothing wrong with the file structure as below is how it display under tomcat after deploy.
css
META-INF
User
pages
login.jsp
customer_list.jsp
WEB-INF
classes
struts.xml
lib
web.xml
But it gives the below error when i try to access the web site,
HTTP Status 404 The requested resource (/LoginApplication/User/Login) is not available.
But i found when tomcat start it gives below exception. But the jar file that claims to say missing is in right path.
Unable to load configuration. - bean - jar:file:/C:/tomcat6/webapps/LoginApplication/WEB-INF/lib/struts2-core-2.3.1.2.jar!/struts-default.xml:54:89
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:449)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
Below is the struts.xml file,
<struts>
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/User" extends="struts-default">
<action name="Login">
<result>pages/login.jsp</result>
</action>
<action name="Welcome" class="loginapplication.action.UserLoginAction">
<result type="redirect" name="SUCCESS">Customers</result>
<result name="input">pages/login.jsp</result>
</action>
<action name="Customers" class="loginapplication.action.ViewCustomerAction">
<result name="SUCCESS">pages/customer_list.jsp</result>
</action>
</package>
</struts>
The error indicates you have got problems with struts.xml file. Pls post that here. Also use Maven for managing dependency. You will never have to bother about folders then.
Pls verify the libraries and versions in your lib.
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.6.jar
xwork-2.0.1.jar
The namespace for the requested action Login is User. The URL should be like the below
http://localhost:8080/MyApp/User/Login
Configure the XML
<package name="User" namespace="/User" extends="struts-default">
<action name="Login">
<result>pages/login.jsp</result>
</action>
</package>
Note that package name and namespace are same

Struts 2 ajax validation unable to find interceptor class jsonValidationWorkflowStack

I'm trying to implement ajax validation in my Struts 2 application. I have included struts2-json-plugin in the build path. Whenever I start the server in Eclipse, I get the following error:
Unable to find interceptor class referenced by ref-name
jsonValidationWorkflowStack - interceptor-ref -
file:/C:/path/struts.xml:15:60
This is what my struts.xml looks like:
<struts>
<constant name="struts.url.includeParams" value="all" />
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
<action name="submitForm" class="action.FormAction" >
<interceptor-ref name="jsonValidationWorkflowStack"/>
<result>results.jsp</result>
<result name="input">index.jsp</result>
<result name="error">index.jsp</result>
</action>
</package>
</struts>
Why is this happening? I'm following the official struts2 ajax validation tutorial at.
You need to extend the "json-default" package.
The "struts-default" package doesn't know about the "jsonValidationWorkflowStack".

Categories