struts.xml namespace causes 404 not found page - java

I have the problem on struts.xml of Struts 2 web project.
I have the login page and after login, the URL will add /secure/*.action.
I tried to have two packages in struts.xml. One is normal / and second is /secure. But it will cause 404 not found page after login. If I make the second package also \ in namespace and call the membersite.action with only / namespace , it will be OK and successfully direct but with only /*.action in URL. I need to have this URL /secure/*.action in infrastructure issue. Is there any hints?
<package name="awip" namespace="/" extends="tiles-default, struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
<global-results>
<result name="sessionTimeout" type="tiles">.logon.LogonForm</result>
<result name="errorRedirect" type="tiles">.errorPage</result>
</global-results>
<action name="logon" class="logonAction" method="displayLogonForm">
<result name="displayLogonForm" type="tiles">.logon.LogonForm</result>
</action>
<action name="doLogon" class="logonAction" method="doLogon">
<result name="displayLogonForm" type="tiles">.logon.LogonForm</result>
<result name="displayMainPage" type="redirectAction">
<param name="namespace">/secure</param>
<param name="actionName">membersite.action</param>
</result>
</action>
</package>
<package name="secureAwip" namespace="/secure" extends="tiles-default, struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
<global-results>
<result name="sessionTimeout" type="tiles">.logon.LogonForm</result>
<result name="errorRedirect" type="tiles">.errorPage</result>
</global-results>
<action name="membersite" method="unspecified" class="membersiteAction">
<result name="displayMembersiteMain" type="tiles">.membersite.Main
</result>
</action>
</package>

The action name should not have an extension .action. If you write it with the action name the action mapper can't find the appropriate mapping for the action's result.
The code
<param name="actionName">membersite.action</param>
should be changed to
<param name="actionName">membersite</param>

Related

How can I map the root path of my web application using struts2?

When I run my strust2 web application, I want to execute an action for the root path [/].
It would be something like an action with no name or just "/", something like:
<action name="/" class="ControllerName" method="execute">
<result name="success">ShowTheFirstPageAfterTheAction.jsp</result>
</action>
...
<struts>
...
<package name="user" namespace="/" extends="struts-default">
<action name="" class="ControllerName" method="execute">
<result name="success">ShowTheFirstPageAfterTheAction.jsp</result>
</action>
// Other actions depending on how you've designed your application
<package
// Other packages (also) depending on how you've designed your application
</struts>

Struts2 Action Class with JSON result

I Have some action classes that return JSON results for my Ajax call,
<action name="sampleAction" class="com.sample.SampleAction">
<interceptor-ref name="defaultSecurityStack" />
<result name="success" type="json">
<param name="noCache">true</param>
<param name="excludeNullProperties">true</param>
<param name="root">result</param>
</result>
</action>
Is it possible to prevent users from accessing that action directly when typed in the url? Thank you so much!

Struts2 param within result not working

It seems I should be able to have parameters in strut.xml defined PER result, and not globally, but I cannot get it to work. Here is what does work :
<action name="actThing" class="Thing" method="execute">
<interceptor-ref name="newStack" />
<param name="parentObject">Parent</param>
<result name="Edit">jspEditThing.jsp</result>
<result name="Add">jspAddThing.jsp</result>
</action>
In this case when Thing.execute gets called, the parentObject variable is set. But here :
<action name="actThing" class="Thing" method="execute">
<interceptor-ref name="newStack" />
<result name="Add">
<param name="location">jspAddThing.jsp</param>
<param name="parentObject">Parent</param>
</result>
<result name="Edit">jspEditThing.jsp</result>
</action>
it does not. Since it works in the first case I certainly have the proper settings/getters, and I don't get any kind of error. What am I missing?
Thanks.
It does not work, and it shouldn't because parameters are applied to the result, not the action. The result is executed after the action, and all parameters should be already set.
The param tag sets a property on the Result object. The most commonly-set property is location, which usually specifies the path to a web resources. The param attribute is another intelligent default.
<action name="actThing" class="Thing" method="execute">
<result name="Add">
<param name="includeProperties">Parent.*</param>
</result>
</action>
Now your Parent object with all its attributes and child objects will be available.

Java Struts call another action after executed action finished

I'm using Java and Struts2.
In my struts.xml configure file i have blow config.
<action name="copyTestSuite" class="testSuiteAction"
method="copyTestSuite">
<result name="success">/WEB-INF/jsp/FormSuccessfulWithOutCloseWindow.jsp
</result>
</action>
After i executed action copyTestSuite, the default page will go to FormSuccessfulWithOutCloseWindow.jsp. and this page is just used to redirect to previous page.
But i want to let it go to another action matched page directly. How can i do this?
Below is another action toUpdateTestSuite i want to redirect to:
<action name="toUpdateTestSuite" class="testSuiteAction"
method="toUpdateTestSuite">
<result name="success">/WEB-INF/jsp/testsuite/updateTestSuite.jsp
</result>
</action>
You can use this <result type="redirectAction"></result>
<action name="copyTestSuite" class="testSuiteAction"
method="copyTestSuite">
<result type="redirectAction">/newAction.action</result>
</action>
You can redirect using something like:
<action name="copyTestSuite" class="testSuiteAction" method="copyTestSuite">
<result type="redirectAction">
<param name="actionName">toUpdateTestSuite</param>
</result>
</action>

Get Request in Struts 2

I am new to struts2. In application there is Action called userLogin. when i enter to url http://servername:9090/appName/userLogin, It should forward the request directly to to /jsp/account/login.jsp. it should not call action method. how can i ensure that when request is through get then forword to loginPage else if Request is Post then it should call the action
Struts.xml is as follows
<?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>
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="default" extends="struts-default">
<action name="index">
<result>jsp/index.jsp</result>
</action>
<action name="userLogin" class="com.ril.tc.action.LoginAction">
<result>jsp/account/login.jsp</result>
<result name="success">jsp/index.jsp</result>
<result name="error">jsp/account/login.jsp</result>
</action>
<action name="ministatement" class="com.ril.tc.action.MiniStatementAction">
<result>jsp/account/ministatement.jsp</result>
<result name="success">jsp/account/ministatementdetails.jsp</result>
<result name="error">jsp/account/ministatement.jsp</result>
</action>
</package>
</struts>
1. You can simply check the request method in action method before the business method executes.
It will add 2-3 lines of java code in action method and a line modification in xml configuration.
2. Or you can strictly restrict the execution of the Action method by using an interceptor.
Interceptor requires a seperate java class and a few lines of xml configuration.
Method 1
a) Java code
public String execute (){ // or your method name
if(ServletActionContext.getRequest().getMethod().equals("GET")){
return "getrequest";
}
........
//your business logic
}
b) xml configuration
<action name="userLogin" class="com.ril.tc.action.LoginAction">
<result name="getrequest">jsp/account/login.jsp</result>
<result name="success">jsp/index.jsp</result>
<result name="error">jsp/account/login.jsp</result>
</action>
Method 2.
a) Java Code
public GetRequestFilterInterceptor extends AbstractInterceptor{
#Override
public void intercept (ActionInvocation action){
if(ServletActionContext.getRequest().getMethod(). equals("GET")){
return "getrequest";
}else{
action.invoke();
}
}
}
b) Xml configuration
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="getUrlFilter" class="packagename.GetRequestFilterInterceptor"/>
<interceptor-stack name="requestFilter">
<interceptor-ref name="getUrlFilter"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
......
<action name="userLogin" class="com.ril.tc.action.LoginAction">
<interceptor-ref name="requestFilter"/>
<result name="getrequest">jsp/account/login.jsp</result>
<result name="success">jsp/index.jsp</result>
<result name="error">jsp/account/login.jsp</result>
</action>
</package>
</struts>
I recommend you to use interceptors, if you want to filter a lot a methods. If you have want to filter only one method, the first one is better.

Categories

Resources