FileUpload Using SmartGWT's DynamicForm & Struts - smartgwt

We are working on SmartGWT 2.2, and Struts2.
I have created a sample form(DynamicForm) which asks to upload file and mapped action class for processing file upload.
I have setup form.setCanSubmit(true);
My call is succefully gets transferred to struts action class and file is getting uploaded also.
struts.xml
<action name="FileUploadAction" class="FileUploadAction" >
<result name="success" type="redirect">success</result>
</action>
But the problem is that control is not getting back to...
form.submit(new DSCallback(){
#Override
public void execute(DSResponse response, Object rawData,
DSRequest request) {
System.out.println("Response: " + response.getHttpResponseCode());
SC.say("back");
System.out.println("BACK...........");
}
});
I read in Smartgwt, Dynamic Form API that,
if this.canSubmit is true, callback is ignored..
As, we are not using DataSource, I have to use this.canSubmit to true.
Response from ActionClass gets struck at 'http://127.0.0.1:8888/success'
So, what is an alternate solution?

you can use hidden iframe and JSNI call that hidden iframe from your server action class....
Thank you.

Related

define a message key in <exception-mapping>?

I'm trying to find a way to configure in struts.xml an error message for each type of exception that can be thrown by an Action class. In an action class I could accomplish something similar by catching an exception, calling addActionError(String), and rethrowing the exception (provided an <exception-mapping> exists). Is there a way to do this through configuration?
As a reference point, this functionality exists in Struts1 with the key attribute on an exception handler - I'm hoping to be able to do something similar.
<exception key="some.key"
type="java.io.IOException"
handler="com.yourcorp.ExceptionHandler"/>
In strut2 also you can define exception mappings. Refer http://struts.apache.org/release/2.1.x/docs/exception-configuration.html. You can have a common error.jsp which displays a message that is looked up based on the class name of the exception.
In Struts2 you can use the following mapping to pass on the key/message to result (result can be a jsp or another action class).
<global-exception-mappings>
<exception-mapping exception="com.test.exception.MyCustomException" result="error">
<param name="param">display.custom.error</param>
</exception-mapping>
</global-exception-mappings>
<global-results>
<result name="error" type="chain">handleAction</result>
</global-results>
<action name="handleAction" class="HandleExceptionAction">
<result name="result">/WEB-INF/jsp/error.jsp</result>
</action>
If it an action class, in case of chaining (if the user wants to process the exception) then you need to have a corresponding attribute in Action class with getters and setters.
public class HandleExceptionAction extends ActionSupport implements
ServletRequestAware, SessionAware {
private **String param**;
private HttpServletRequest httpRequest;
private static final Log LOG = LogFactory.getLog(InputAction.class);
public String execute(){
LOG.debug("inside excute().....");
LOG.debug("Parameter passed:" + param);
System.out.println("Parameter passed:" + param);
return "result";
}
I am using spring injection, hope it will hep you.

Can be load and update data in one page using struts 2?

I wonder whether i can use 1 action to load data and update it?
It mean i have a page call about.jsp, manager will click a link to access this page. after redirect into this page, this page load content of about and update content if user edited data but two tasks can be use 1 action.
my i dea is, user use a action call AboutAction to load data to about.jsp page, after end user edited data on about.jsp and click submit, it will send data to AboutAction and update it?
Can i do that? and how?
First thing What you asked is one of the beauty of Struts2
xml :
<action name="AboutAction" class="AboutAction" method="load">
<result>showMe.jsp</result>
<result name="input">about.jsp</result>
</action>
<action name="AboutAction" class="AboutAction" method="update">
<result>showMe.jsp</result>
<result name="input">editsuccess.jsp</result>
</action>
Action :
public String load(){
//logic to load
return SUCCESS;
}
public String update(){
//logic to update
return SUCCESS;
}

Error not being passed back to struts jsp

This worked a month ago and now i reload the code and it doesn't work. here is my struts snip
<action name="checkManager" class="CheckAction">
<result>/pages/check_manager.jsp</result>
</action>
<action name="submitFile" class="SubmitFileAction">
<result name="success">/pages/submit_file.jsp</result>
<result name="error">/pages/check_manager.jsp</result>
</action>
So, the first page is checkManager. it has a button that calls the submitFile action. When that fires, it checks a password, if it fails the action class sends an error result. Now a month ago the result error above would redirect the user back to the original check_manager.jsp page complete with data that was loaded originally and the error message. Today, it redirects the user to the raw check_manager.jsp page. no data, its like the servlet never fired and its just rendering a blank jsp page. I checked the source history and nothing has changed in this application.
I don't understand why this would stop working, can anyone give me an idea? i have to present this to the client in about an hour and a half so i'm really stuck here.
note, i tried changing the error to this
<result name="error" type="redirectAction">checkManager</result>
and I get my data back, but i loose my error message which isn't a good thing. ugh, any ideas?
if (enteredHash.equals(storedHash)) {
_log.debug("The password matched");
UserSession<User> userSession = (UserSession<User>) session
.get("user");
#do logic#
} else {
_log.debug("The password didn't match");
addActionError("The password you entered was incorrect, nothing was sent to the bank.");
return ActionSupport.ERROR;
}
return ActionSupport.SUCCESS;
my jsp
<s:if test="hasActionErrors()">
<div class="errors">
<s:actionerror/>
</div><br>
</s:if>
again, this worked perfectly a month ago. All i did was check out that branch which has been untouched and deploy it. Now it doesn't work.
A quick fix could be to add this to getCheckList in the CheckAction:
public Collection<Check> getCheckList() {
ActionContext.getContext().getSession().put("checkList", checkList);
return checkList;
}
and add this to the SubmitFileAction:
public Collection<Check> getCheckList() {
return (Collection<Check>) ActionContext.getContext().getSession().get("checkList");
}
This is not a good permanent solution but should get you where you need to get in a crunch!

Send data from client side JS code to Struts 2 ActionSupport class

I've seen some post about sending and building jgGrids from actions registered on struts.xml web application that uses this libraries. But I've not seen yet any about how to catch the processed data from a grid. My code:
jQuery("#bedata").click(function(){
jQuery('#rowed3').jqGrid('restoreRow',lastsel);
var gridData = jQuery("#rowed3").getRowData();
var postData = JSON.stringify(gridData);
alert("JSON serialized jqGrid data:\n" + postData);
$.ajax({
type: "POST",
url: "CargaTabla.action",
data : {
jgGridData: postData,
customData: "someinfo"
},
dataType:"json",
contentType: "application/json; charset=utf-8",
success: function(response, textStatus, xhr) {
alert("success");
},
error: function(xhr, textStatus, errorThrown) {
alert("error");
}
});
});
id = betadata button send data to "CargaTabla.action", an action registered in Struts 2 configuration file. Data is properly serialized as a JSON string. ClassActionImpl is the class that catch this action, is there any way to get the information sended by Javascript? Has Struts 2 any way to get this data without use JSP request or something like that, just Java code in the same ClassActionImpl.execute()? Some hidden parameter?
Thank you.
struts.xml
<action name="CargaTabla" method="guardarUsuario" class="org.json.JSONRespuestaTabla"> <result name="success" type="json">
<param name="includeProperties">jgGridData</param>
</result>
</action>
Ok Umesh. I new in this community I am not able to score you, yet. :( Sorry. I appreciate so much your effort.
Well, I fixed the issue. It seems like struts doesnt like 'POST' method to autofill attributes:
$.ajax({
type: "GET",
Instead:
$.ajax({
type: "POST",
And it access automatically to setter methods anyway its name. If I have an attribute name "customData" that means it will automatically access to its setter method and fill with data sended from client. If there is no attribut called like a certain parameter, Struts 2 just ignore the data. There is no need to configure anything else in struts.xml file. Just the action:
<action name="CargaTabla" method="guardarUsuario" class="org.json.JSONRespuestaTabla">
<result name="success" type="json"/>
</action>
This is very interesting and strange at once; no need to declare any parameter. -param- in struts.xml tag is used only to SEND data to web client. You cannot filter if you want or not to fill some attribute of the ActionClass. I would like to know more about data from client management in Strut2. Maybe there is some interceptor to help on that.
Thank you again.
Raph
Well Struts2 has build in mechanism to receive data being sent by the request to the action class.
In your case i believe you are sending following data\
jgGridData: postData,
customData: "someinfo"
Not sure in what format this code is sending data to action especially jgGridData.But for the second parameter all you need to do is to define a property in your action class with name customData and its getter and setter, struts2 param interceptor in request execution process will look for property with similar with the request parameter name and will try to set the value in respected property in your action class.
public class ClassActionImpl extends ActionSupport{
private String customData;
private String jgGridData; // I am assuming data as string you can change it as per your data type
//getter and setter for both above properties
public String execute() throws Exception{
//can use both the above properties here as they will be filled by fraework
}
}

Struts interceptor giving a stream result

I got an interceptor that I'm trying to get to output a stream when a certain action is calling. This is part of my code in the inteceptor:
InputStream inputStream;
public String intercept(ActionInvocation invocation) throws Exception
{
if (currAction.contentEquals("actionToTest"))
{
String result = "TRUE";
inputStream = new ByteArrayInputStream(result.getBytes("UTF-8"));
return "resultToGiveStream";
}
}
inputStream has got it's own getters and setters.
And in struts.xml:
<global-results>
<result type="stream" name="resultToGiveStream">
<param name="contentType">text/plain</param>
<param name="inputName">inputStream</param>
</result>
</global-results>
But when I call actionToTest I only receive this in my console:
2011-maj-18 11:19:16 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
ALLVARLIG: Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
Is it a lost cause to get it to output what I want? I haven' found anyone doing anything similar.
This code is an atempt for a workaround for my other question.
Struts2 is looking for the getInputStream() method on your action and it isn't finding it.
You could try placing the inputStream on the stack manually from within the interceptor. Something like:
invocation.getInvocationContext().put("inputStream", inputStream);

Resources