I am forwarding to an action by giving as
<forward name="sample" path="/sample.do?button=default" />
i want to add one more attribute in path and i used:
<forward name="sample" path="/sample.do?button=default&value=text" />
...and I am getting org.xml.sax.SAXParseException
Any solution for it?
As Omnipresent and Shashi already said, you must encode the ampersand as & so that the forward definition looks like this:
<forward name="sample" path="/sample.do?button=default&value=text" />
However, the URLs defined in your struts-config.xml are frozen, and if you need to dynamically change a value or add another parameter, you can do it by creating a new ActionForward based on the forward that you get from mapping.findForward().
ActionForward forward = mapping.findForward("sample");
StringBuilder path = new StringBuilder(forward.getPath());
path.append("?id=");
path.append(someobject.getId());
path.append("&value=");
path.append(getValue());
return new ActionForward(path.toString());
<forward name="sample" path="/sample.do?button=default&value=text" />
You can pass multiple parameter in forward.
But you have to use '&' instead of '&'.
To be strictly accurate, the parser handler should scan the buffer for ampersand characters (&);and left-angle bracket characters (<) and replace them with the strings & or <, as appropriate.
So, the forward statement will be as
<forward name="sample" path="/sample.do?button=default&value=text" />
you can not use '&' in struts-config.xml
value=text should be passed from your action...not the way you are trying to pass it (in url).
Your forward tag must be associated with an action. that action should have a getter called value which returns 'text'.
what way sample.do will have access to that varable.
Related
So in my targets file, I've got a line that looks like this:
<XmlPeek Namespaces="" XmlInputPath="file.xml" Query="/data/#AttributeOne">
<Output TaskParameter="Result" ItemName="my_AttributeOne" />
</XmlPeek>
in "file.xml", I have:
<data AttributeOne="abc" AttributeTwo="def" />
it also reads a few other attributes.
When the attribute has data, everything works fine... but when I leave AttributeOne as an empty string (""), XmlPeek blows chunks with the following error:
The "XmlPeek" task's outputs could not be retrieved from the "Result" parameter. Parameter "includeEscaped" cannot have zero length.
if I remove the attribute ENTIRELY, it works fine (the resulting item is obviously and understandably blank)
The question is... how can I DETERMINE, WITHOUT blowing chunks, the value of a blank attribute... whether by pre-testing for a value, or by correctly handling the blank, or some other means.
CONSTRAINT: the only real requirement is to stick to the built-in tasks (XmlPeek)... I'm aware of XmlRead in the community tasks... for various reasons, I want to use out-of-the-box tasks.
Thanks in advance!
The error happens because an empty string is being used as the Item Identifier. I guess identifiers cannot be the empty string. If you remove the attribute then the result is null and no Item is created so that's why that doesn't throw an error.
Maybe try return the result as a Property instead of an Item.
If you do not need to distinguish between the attribute being omitted versus having an empty value, you can prevent the error by inserting the condition [#AttributeOne!=''] into the query as follows.
<XmlPeek Namespaces="" XmlInputPath="file.xml" Query="/data[#AttributeOne!='']/#AttributeOne">
<Output TaskParameter="Result" ItemName="my_AttributeOne" />
</XmlPeek>
I am new to Struts and I Don't Understand Struts Action Parameters, specifcally: Name,Validate,Input and Redirect="true"
Example:
*#struts.action name="activation" path="/activation" validate="false" parameter="activation"
*#struts.action-forward name="activationStart" path="/activation.html" redirect="true"
Please try to answer me in terms of above example.
So basically you refuse to read the Struts 1 documentation?
name
The name of the ActionForm bean for this action.
validate
Should validation run?
input
The input page for the form, usually used to return to the form on a validation error.
redirect
When this forward is returned should it be a redirect or a forward?
Name
Name is the name of the formbean which is associated with you JSP. In your struts-config you will have something like this
<form-beans>
<form-bean
name="activation">
<form-property name="name" type="java.lang.String"/>
</form-bean>
</form-beans>
validate
You have validator framework which has some validations in place, like checking the name field for null value and so. You tell your struts whether the validator should work or not. If validation=false, then validation is not performed in your current jsp
input
input is the input jsp. If there are any validation errors, the execute method of the action will not get called; instead the control will go back to that ***.jsp. But you have not specified any input in your example
redirect
Whether to redirect or just forward
The below piece of code was written in struts-config file.but i am not able to understand it.
<action path="/showWelcome"
type="com.code.base.presentation.struts.actions.StrutsIoCAction"
name="LoanDetailPageLoadForm"
parameter="GET_WELCOME_PAGE"
input="welcomePage"
validate="false"
scope="request">
<set-property property="requestDTOKeyName" value="ItemDataRequest" />
<set-property property="responseDTOKeyName" value="ItemDataResponse" />
<set-property property="exceptionDTOKeyName" value="ProfileSekerException" />
<set-property property="businessServiceId" value="ItemsDataMgmtService" />
<forward name="success" path="welcomePage" />
<forward name="failure" path="sysError" />
</action>
My question is
what is the usage of path attribute?
what is the usage of parameter attribute?
what is the usage of input attribute?
what is the usage of <set-Property>?
Help me guys on this.
Note:
as per my understanding there should be "showWelcome.jsp" page in the application but it is not there.(then what is use of that?)
It specifies where the action is mounted. For example, this action would respond on http://yourservice.dom/showWelcome.
Parameter is the string you get by calling ActionMapping.getParameter(). Any string you want to pass to your action.
Input is a path where the user would be redirected if he fills the form incorrectly. As there's validate=false, I'd say that would never happen.
Obviously, it sets a property on com.code.base.presentation.struts.actions.StrutsIoCAction. I think it calls setter, i.e. it would call setRequestDTOKeyName(), setResponseDTOKeyName() etc.
But if you're going to use struts for a considerable time, QA won't get you far, read some docs on struts' config file.
Following on from #Alamar's response...
There is no showWelcome.jsp. "/showWelcome" is the URL, but that does not correspond to the name of any actual filename on the server. If this action's configuration contained a line like this:
<forward name="success" path="showWelcome.jsp" />
Then it would mean that if the action class (StrutsIoCAction) returns success, a file called showWelcome.jsp would be executed. However, as you can see, the actual configuration is a forward to "welcomePage", which is probably not a file but instead the name of another action (also defined in struts-config).
Note: "forward" just means that execution is passed to this other action, it does not mean that the user is redirected to another URL.
I'm using the json plugin that comes with struts 2 (json-lib-2.1.jar) and trying to follow the website to set it up.
Here's my struts.xml
<struts>
<package name="example" extends="json-default">
<action name="AjaxRetrieveUser" class="actions.view.RetrieveUser">
<result type="json"/>
</action>
</package>
</struts>
but I get this warning:
SEVERE: Unable to find parent packages json-default
Is there something else I'm supposed to do?
Edit:
I added this method to my RetrieveUser:
public Map<String,Object> getJsonModel()
{
return jsonModel;
}
And my struts.xml looks like this:
<struts>
<package name="example" extends="json-default">
<action name="AjaxRetrieveUser" class="actions.view.RetrieveUser">
<result type="json"/>
<param name="root">jsonModel</param>
</action>
</package>
</struts>
However, I don't think the response is going from the RetrieveUser class to the javascript. I'm using firebug and no request gets sent.
I believe that net.sf.json-lib is just a toolset you can use in your Java to build up JSON-ready objects, suitable to be returned by actions such as you describe.
Probably, you need to include struts-json-plugin - make sure its version matches your struts version.
I notice also that as written, your action will attempt to return RetrieveUser, serialized. Most implementations I've done/seen specify the root object to be returned, by adding
<param name="root">jsonUser</param>
Under the tag, and define this method in RetrieveUser
public Map<String, Object> getJsonUser()
[This is mentioned in the Sruts2 doc]. Hope that helps.
[edit] I use Map - you could also use the object structures provided by json-lib instead.
Re: Your edit. Probably need to see your calling javascript. And probably I will suggest that you make sure you have both a success and an error handler. Can you debug/log to show that the method is being called in java ? Do your logs show anything ? This is usually some sort of error....
I use Struts v1.3 and have following input form:
In struts-config.xml:
<form-bean name="testForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="displayName" type="java.lang.String" />
</form-bean>
In validation.xml:
<form name="testForm">
<field property="displayName" depends="required">
<arg key="input.displayName" />
</field>
</form>
How do I trim value of "displayName"? How do I trim values of all "java.lang.String" input fields of the form?
You may have a chance to trim the string right at the moment, the request processor updates the data from the input fields to the form. This was not tested, but what happens when you modify the setter setDisplayName(String displayName) to something like
public void setDisplayName(String displayName) {
this.displayName = displayName.trim();
}
This is not a very good solution, because it migrates logic into a setter.
regards
If you want to use trim for validation purposes, I think the proper way is to create and use your own (or extend an existing) validator for required fields that takes trim into consideration.
For an example, you can use this page: http://struts.apache.org/1.2.4/userGuide/dev_validator.html, the section "Pluggable Validators".
If you want to use trim for trimming the String values before using them in your business logic, you can extend org.apache.struts.validator.DynaValidatorForm and overwrite the methods that retrieve values, like get(String name), getString(String name) and so on. After that, you use your class in the form-bean declarations.
If you don't mind having String manipulation logic in your Form class, you can try the StringUtils methods in Apache's Commons Lang JAR:
StringUtils JavaDoc
This will let you trim your Strings in a number of specific ways, whether you want to trimToEmpty, trimToNull, etc. This means you have access to null-safe methods, which can be useful with some values.
Alternatively try using javascript regexp in the jsp that will trim onfocus or onblur
< html:text name="testForm" property="displayName" onfocus="javascript:this.value=this.value.replace(/^\s+|\s+$/g,'')" onblur="javascript:this.value=this.value.replace(/^\s+|\s+$/g,'')" />