Before starting, I'm sorry for my bad English, because English is not my first language. However, I have a problem with Struts 2 redirecting.
One part of my Struts.xml file is like this:
<package name="login" namespace="/" extends="struts-default">
<action name="login" class="Action.loginAction">
<result name="success" type="redirectAction">
<param name="actionName">search</param>
<param name="namespace">/</param>
</result>
<result name="error">/login.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
I have a loginAction that's responsible for logging in the user (no matter what is the content of this file). When the result of this action is success, I want to redirect to search action. Everything works fine, but my problem is, when I redirect to search action, the URL looks like this: http://localhost:8080/MyWebApplication/search.action
I want to have the .action removed from the URL, when redirecting to a particular action. What do I need to change to fix this?
Thanks for your attention.
By default struts2 framework will add .action you can override it by following configuration.
<constant name="struts.action.extension" value=""/>
For ref:
http://struts.apache.org/release/2.3.x/struts2-core/apidocs/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.html
Related
I used Struts 2.5.16,follow code is struts.xml content:
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="authorizeInterceptor" class="web.comm.Interceptor.authorizeInterceptor"></interceptor>
</interceptors>
<action name="home">
<result type="dispatcher">/view/index.jsp</result>
</action>
<action name="exp" class="web.comm.userAndAuthorize.commUserOp" method="jsonChangePass">
<interceptor-ref name="authorizeInterceptor"/>
<exception-mapping result="Exception" exception="java.lang.Exception"/>
<result name="Exception" type="chain">/view/UserAndAuthorize/error.jsp</result>
<allowed-methods>jsonChkLogin,jsonChangePass,createUser</allowed-methods>
</action>
url is http://localhost/exp
follow code is method of jsonChangePass()
public String jsonChangePass()throws Exception{
int b=0,a=1;
int c=a/b;
follow content is chorme showed:
Struts Problem Report Struts has detected an unhandled exception:
Messages: / by zero ......
see the web showing content, action is already working.why the exception-mapping not worked?thanks for ervery one help.
Since you're not running the exception mapping interceptor, it won't work.
Once you define <interceptors> in a package you must define all the interceptors: this is not an additive element, this is a declaration of the interceptors used in that package.
IIRC you can use an <interceptor-ref> to get the defaultStack (or whichever you actually intended to use) and add your authorizeInterceptor.
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>
I am having an issue with the Message Store Interceptor in Struts 2. I have the following action in my struts.xml file:
<action name="rfi" method="add" class="org.test.action.RfiAction">
<interceptor-ref name="store">
<param name="operationMode">AUTOMATIC</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="error">../display/irrt/rfi.jsp</result>
<result name="input">../display/irrt/rfi.jsp</result>
<result name="success" type="redirectAction">
<param name="actionName">rfis</param>
<param name="namespace">/irrt</param>
</result>
</action>
When the action returns successfully, it redirects to the action listed and the success message is properly saved and retrieved by the interceptor.
However, when an error occurs, there is no redirect and it goes to the listed JSP page but the error doesn't show up (all other data does). It's like the MessageStoreInterceptor wipes out the contents of the error variables when it runs so that if no redirect occurs, the current action no longer has the error message.
This happens when the interceptor is set to either STORE or AUTOMATIC mode (even though the interceptor shouldn't even run if it's in AUTOMATIC mode and the result doesn't include a redirect).
My code only ever adds errors or messages. It never deletes them. The action code is below:
private String add()
{
try
{
// add the rfi entry
this.rfiService.addRfi(this.rfiEntry, this.getLoggedInUser());
}
catch(ConnectionException e)
{
this.addActionError("Could not add RFI entry.");
e.printStackTrace();
return ERROR;
}
// set success message
this.addActionMessage("RFI entry added.");
return SUCCESS;
}
This is the code in the JSP being used to display the messages:
<s:if test="hasActionErrors() == true">
<s:iterator value="actionErrors">
<p class="text"><b><font color="red"><s:property /></font></b></p>
</s:iterator>
</s:if>
<s:elseif test="hasActionMessages() == true">
<s:iterator value="actionMessages">
<p class="text"><b><font color="green"><s:property /></font></b></p>
</s:iterator>
</s:elseif>
Any help with this issue would be greatly appreciated.
<result name="success" type="redirectAction">
<param name="actionName">rfis</param>
<param name="namespace">/irrt</param>
</result>
The redirectAction rfis should also go through the store interceptor.
<action name="rfis" class="..." method="...">
<interceptor-ref name="store">
<param name="operationMode">AUTOMATIC</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result ..../>
</action>
I was missing the include on this particular page that included the errors.
Moral of the story: don't assume code that's on every single other page is on the one that's not working.
I have developed a application using struts 2.0. On the server log I am getting certain warning lines. I want to fix this warnings but I am not able to do it.
[WARN] No configuration found for the specified action: '/' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
In my struts.xml file I have something like this
<struts>
<constant name="struts.devMode" value="true" />
<package name="basicstruts2" extends="struts-default" namespace="/">
<global-results>
<result name="error">/error.jsp </result>
</global-results>
<action name="Login" class="com.login.LoginAction">
<result name="input">/login.jsp</result>
<result name="success">/dashboard.jsp</result>
</action>
</package>
</struts>
In the jsp page I am calling following javascript function on click of a button
function submitloginform()
{
document.loginform.action = "Login.action";
document.loginform.submit();
}
Can anyone please help me for this.
just keep action name in struts.xml and action of form to be submitted same.
function submitloginform()
{
document.loginform.action = "Login";
document.loginform.submit();
}
This warning message has nothing to do with the javascript or the form submission. It happens when the tag is rendered to the it's representation in HTML.
You must be missing the action attribute in your in your case. Make sure that your code looks like the following.
<s:form name="loginform" method="post" action="Login">
If you are trying to render a form in another namespace, for example: /public, you should be doing
<s:form name="loginform" method="post" action="Login" namespace="/public">
instead of
<s:form name="loginform" method="post" action="/public/Login">
I have designed an application using struts2, and need to remove the extensions for example
www.myweb.com/register.action
How to remove the .action so when I click on register it call www.myweb.com/register without action extension?
I use the following
Register
I've found the answer. I should change the struts configuration as following:
<struts>
<constant name="struts.action.extension" value=""/>
<package name="default" extends="struts-default">
<action name="register">
<result type="tiles">register</result>
</action>
</package>
</struts>