I want to do something like this:
<a href"page1/login.action"> Link </a>
<a href"page2/login.action"> Link </a>
<a href"pagen/login.action"> Link </a>
Then every subfolder will use the same login, then I can create dynamic subfolder
How I have to configure struts.xml?
This doesn't work
<package name="default" extends="struts-default" namespace="/*/">
<action name="login" class="package/myclass">
...
</package>
Any idea?
We cannot use wildcards in namespace. But u can use wild cards for action mappings.
Use struts url tag in jsp and use wildcards in the action names in struts.xml. see the reference http://struts.apache.org/2.2.3/docs/wildcard-mappings.html
'> Link
'> Link
Hope this will help you.
Finally I do:
<package name="default" extends="struts-default" namespace="/">
<action name="login" class="package/myclass" method="initCampusList" >
<result name="success" >/user/pickUser.jsp</result>
I have to put the absolute url of the result
Thanks for the answers
Related
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.
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
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.
My login.jsp is in the web folder.
And action for this is specified as :
struts.xml
<package name="admin" extends="struts-default" namespace="/secure">
<action class="actions.LoginAction" name="authenticateUser">
<result name="success" type="redirect">index</result>
<result name="input">/login.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
login.jsp
<s:form action="secure/authenticateUser" method="post">
</s:form>
========================
At first request it works.
but if validate() method of action returns errors then it creates url as:
"secure/secure/authenticateUser" for the Form action attribute.
I also tried <s:url> tag but still same problem can anybody help me.
or may provide alternate solution for this.
Your action attribute on the form tag possibly has a wrong name. Use
<s:form namespace="/secure" action="authenticateUser" method="post">
I'm using Struts2. I run a action named hello1.
I input the url: http://localhost:8081/MyStruts2/hello1, it works.
And I tried another url:http://localhost:8081/MyStruts2/hello1.action, it also works?
I don't think we need the ".action".
So why do some people add the suffix ".action"? Is it necessary?
And can we config the ".action" as another suffix such as ".go"?
.action is the default suffix added by Struts. To change it from '.action' to '.go'. Use the below in XML to change the suffix.
<struts>
<constant name="struts.action.extension" value="go"/>
<package name="default" namespace="/" extends="struts-default">
<action name="SayStruts2">
<result>pages/printStruts2.jsp</result>
</action>
</package>
</struts>