Form validation in Struts2 - java

Page contains form which looks like below.
1) If I insert to form validate="true", then after page load this form submit request to mail action (at this point validation xml was not created yet).
2) After validation xml was created (in the same pakage with action), then action which control request to this page returns 404 page.
Did I miss something?
<s:form action="mail" method="post">
<s:textfield name="name" key="Your name" size="20" />
<div style="clear: both;margin-top:10px"></div>
<s:textarea label="Comment" name="comment" cols="65" rows="5"/>
<s:submit method="mail" key="Send" align="left"
style="width:100px; height:35px; margin-top:20px"/>
</s:form>
validation xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="name">
<field-validator type="required">
<message>Please enter a user name</message>
</field-validator>
</field>
<field name="comment">
<field-validator type="required">
<message>Please enter your message</message>
</field-validator>
</field>
</validators>
If I do not add validation then all is working as it should.
struts.xml
<?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" namespace="/" extends="struts-default">
<global-results>
<result name="Exception">/error404.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Throwable" result="Exception" />
</global-exception-mappings>
<!-- loads page -->
<action name="main"
class="com.user.action.LoginAction" method="main">
<result name="success">/main.jsp</result>
</action>
<!-- mail action -->
<action name="mail" class="com.user.action.LoginAction" method="mail" >
<result name="success">/main.jsp</result>
</action>
</package>
</struts>

INPUT is one of the predefined results made available by Struts2;
Basically, if your Action sends parameters that are incorrect (conversion errors, like if you send a "abc" to an Integer Action variable), or that don't pass the validation, the Workflow Interceptor return the INPUT result and follow the path specified in the struts configuration for that Action.
Your problem is that you have not defined any INPUT result for your Actions, while you always should.
You can also set a global input result as a fallback, but be careful with that... generally, the page you want to go in case of an INPUT is the same from where the request has been sent.
In case of an INPUT result, your Action method (eg. execute()) is not executed, so if you load common data in that method, like select boxes content, it won't be available anymore.
Read this answers to fully understand what this implies and how to make it work:
How do we repopulate controls when validation fails
Detailed Workflow of the INPUT result processing

Related

After migrating to struts 2.3.24 from struts 2.1.8, no response to action on login page

My application displays the login page, but cannot login.
On the login page, user enters the username and password but on pressing the login button nothing happens.
We have in our JSP :
<s:form id="loginForm" name="loginForm" action="login" namespace="/framework">
The struts-framework.xml file has the action defined as follows:
<global-results>
<result name="loginShow" type="redirect">loginShow.action</result>
<result name="toMain">/page/framework/main.jsp</result>
<result name="logout">/page/framework/logout.jsp</result>
</global-results>
<action name="login" class="com.abc.action.framework.LoginAction" method="loginCheck"> </action>
In our logs we get the Request start for the action framework/login.action, but no response is obtained thereafter and same is evident from the logs.
For reference I have included the following to my struts.xml after migrating struts to 2.3.24 :
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
Thanks for all your efforts. It was an issue with struts-convention plugin.Works well for me on removing this plugin and mapped dependancies in struts.xml

Struts2: Setting constant struts.action.extension to "," not working

I am working on struts2-archetype-starter. Got a demo struts 2 project by executing:
mvn archetype:generate -B -DgroupId=demoStrutsStarter -DartifactId=DemoStrutsStarter -DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts2-archetype-starter
I modified my struts.xml and added a struts.action.extension constant declaration. Now it looks like this:
<?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.action.extension" value=","/>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<package name="myPackage" extends="struts-default">
<default-action-ref name="index" />
<action name="index" class="demoStrutsStarter.IndexAction">
<result>/jsp/index.jsp</result>
</action>
<action name="helloWorld" class="demoStrutsStarter.HelloWorldAction">
<result name="input">/jsp/index.jsp</result>
<result>/jsp/helloWorld.jsp</result>
</action>
</package>
</struts>
But, I am getting a 404 error when I type:
http://localhost:8080/DemoStrutsStarter/index
I am not sure where I am wrong. Can anybody please help me find out why I am unable to call actions without .action extension?
Try adding a namespace to your package:
<package name="myPackage" namespace="/" extends="struts-default" >
Try this out in both results
result 1
<result>/WEB-INF/jsp/index.jsp</result>
result 2
<result>/WEB-INF/jsp/helloWorld.jsp</result>
else check your package name is correct or not.
by default, it's name default
try this links. it may help
1.stack overflow link 1
2.link 2 mkyong
3.stack overflow link 3

Struts2- Persistence of objects values between different actions in the same Action class

Scenario: On click of a Submit button I have unmarshalled the XML to Java and then displaying the Java objects(created a formbean and mapped the JAXB java Objects to this formbean - PolicyForm and using this formbean to display in JSP) in the JSP. This is working fine.
Issue: I have one more button -Calculate on the same page with in the same form of Submit button. On click of the 'Calculate' button I need to get the acordform values which was unmarshalled during the click of Submit button. Here I am not able to get the acordform values instead I am getting the new object of acordform.
When I have googled I see - Struts 2 doesn't have thread-safety issues as "Action objects are instantiated for each request".
Please let me know here when I am sending the request again is the Action object- acordform being instantiated everytime? If yes, how can I avoid this? Since I need the acordformobject values even in the next request too.
Action Class:
public class RateAction extends ActionSupport implements
ServletRequestAware,SessionAware {
/* ... */
// ACORD xml form bean
private ACORD acordform = new ACORD();
//To display the values in the JSP
private PolicyForm policyForm;
public ACORD getAcordform() {
return acordform;
}
public void setAcordform(ACORD acordform) {
this.acordform = acordform;
}
public String doSubmit() {
/*...Unmatshalling from XML to Java -acordform is done..*/
}
public String doRateSubmimt()
{
/*..trying to get the acordform values which are being set
previously in doSubmit() method...*/
}
}
Strust.xml:
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="default" extends="struts-default" namespace="/" >
<action name="fileUploadAction"
class="com.main.common.action.RateAction" >
<interceptor-ref name="fileUpload">
<param name="allowedTypes">text/xml</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="params"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="basicStack"/>
<result name="success">First.jsp</result>
<result name="input">First.jsp</result>
</action>
<action name="submitAction" class="com.main.common.action.RateAction" method="doSubmit">
<result name="success">First.jsp</result>
</action>
<action name="rateAction" class="com.main.common.action.RateAction" method="doRateSubmit">
<result name="success">First.jsp</result>
</action>
</package>
JSP:
<s:form id="rtr" action="fileUploadAction" method="POST"
enctype="multipart/form-data">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<s:file name="uploadFile" label="Upload Request XML" />
<s:submit value="Submit" action="submitAction" onclick="displayDetails();" />
<s:hidden name="submitSuccess" />
<s:submit value="Calculate" action="rateAction" />
</tr>
</table>
</s:form>
Can't you perform the operation in one click ? I mean Submit the XML, and draw the result with the unmarshalled XML...
If no, since you need to share that in-memory XML (not saved on HD nor DB, just posted to the Action) between multiple requests / actions, put it in the Session Map by implementing SessionAware in your Action.
Note that you are using too much Interceptors...
This should be enough:
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedTypes">text/xml</param>
</interceptor-ref>

Current URL /web/guest/HTML generates exception: null in Liferay 6.0.6

I develop an example of Struts2 with Jasper Reports and every thing work fine. In my index.jsp I have
index.jsp:
<body>
<a href="HTML.action" target="_blank" >click to generate pdf report</a>
</body>
struts.xml:
<struts>
<package name="default" extends="struts-default,jasperreports-default">
<action name="HTML" class="com.tutorialspoint.DataBeanList" method="exporte">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
but when I want to do that with Liferay 6.0.6 I got this error:
INFO [PortalImpl:3829] Current URL /web/guest/HTML.action generates exception: null
In my struts.xml I have:
<!-- Action pour générer un rapport pdf -->
<package namespace="/reporting" extends="struts-portlet-default,json-default,jasperreports-default" name="reportingview">
<action name="HTML" class="com.xxxxxx.struts2.actions.genererQPpdf" method="exporte">
<result name="success">/JSPs/reporting/resultQP.jsp</result>
</action>
</package>
so any one can help me.
Better use the struts tags to exclude errors in the code. For example url tag could be used to construct the correct link that point to the action.
<a href="<s:url namespace="/reporting" action="HTML"/>" target="_blank" >click to generate pdf report</a>

How to remove warning from the server log: [WARN] No configuration found for the specified action: '/' in namespace: ''

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">

Categories

Resources