Struts namespaces and WEB-INF folder [duplicate] - java

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

Related

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's the difference between url "login" and "login.action"?

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>

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

Multiple Struts 2 configuration files

I have multiple XML configs.
struts.xml
<struts>
<include file="struts-user.xml" />
<package name="baseInterceptors" extends="struts-default">
<interceptor name="...">
...
</interceptor>
...
</package>
<package name="default" extends="struts-default,baseInterceptors">
<action name="...">
...
</action >
...
</package>
</struts>
struts-user.xml
<struts>
<package name="user" extends="struts-default,baseInterceptors">
<action name="...">
...
</action >
...
</package>
</struts>
But the interceptors from baseInterceptors are not available in struts-user.xml. How can I solve this problem?
Configuration files are processed in order: The <include> is processed before the baseInterceptors and default packages are processed (read: defined).
In other words, the included file depends on pacakges that are not yet defined.
The package configuration docs explain this in a note near the top with an exclamation point next to it.
Unrelated, but you've duplicated some configuration, which is misleading/uncommunicative: baseInterceptors already extends struts-default, so there's no need to extend both. Consider creating something like an application-default package so it's obvious everything in the app should extend from it. This eliminates unnecessary duplication and communicates your intent.

Struts2 namespace complex configuration

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

Categories