Getting 404 error when using dojo.xhrPost + struts? - struts2

I am not sure why this was happening. I have a struts form that will post to my struts action's method:
<s:form data-dojo-type="dijit/form/Form" action="SaveRec" method="POST"
enctype="multipart/form-data" theme="simple" id="frmRecord">
<div class="item">
...
Basically, I just hit the link htp://example.com:8080/Test/SaveRec in browser and I will get a workable page, but after using these code, I will get the post 404 error(POST http://example.com:8080/Test/SaveRec.action 404 (Not Found) ):
dojo.connect(dojo.byId('btnSave'), 'onclick', function(event){
// The parameters to pass to xhrPost, the message, and the url to send it to
// Also, how to handle the return and callbacks.
var xhrArgs = {
form: dojo.byId("frmRecord"),
handleAs: "json",
load: function(data){
console.log("Message posted. " + data);
},
error: function(error){
console.log("Message failed to post, " + error);
}
}
console.log("Message being sent...");
// Call the asynchronous xhrPost
var deferred = dojo.xhrPost(xhrArgs);
});
PS: I can run invocation.invoke() and print out message in my interceptor
UPDATE:
OK, I think the problem is the return type of action, I have added the interceptor into the action, however, I still cannot use the getter to retrieve the value:
<struts>
<package name="testJson" extends="json-default">
<action name="SaveRec" class="com.xyz.Test" method="saveRec">
<interceptor-ref name="json">
<param name="contentType">application/json</param>
</interceptor-ref>
<result name="success" type="json">
<param name="root">saveResult</param>
</result>
</action>
...

Related

Primefaces args.validationFailed is true but no validation message

I submit some fields with a button:
<p:commandButton value="#{msgs['label.button.editPropertiesSave']}" update=":editPropertyFormId:editPropertiesDialogContentId #(.resultlistActionGrid) " process=":editPropertyFormId:editPropertiesDialogContentId #this" action="#{editPropertyBL.save()}" oncomplete="resetDocValueChangedIfValidationOk();" />
function resetDocValueChangedIfValidationOk(args) {
if (args) {
if (!args.validationFailed) {
resetDocValueChanged();
}
}
}
If I submit it with valid input the backing bean method editPropertyBL.save() is called but args.validationFailed is true and my javascript is not executed.
I have a p:messages autoUpdate="true" in the page which shows an info message about the saving but it does not show any validation error.
Why can args.validationFailed be true at this situation?
Regards
Oliver

Displaying Bar chart in different window

I am trying to display a bar chart on click of button on a page which is test.jsp. I am able to display the chart,but it opens as a image in the same browser where my test.jsp is.I want to display it in a new Window ,how can I do that. I am using Struts2 and Jfreechart:
Following is my action :
public class TestAction extends ActionSupport{
public String execute() throws Exception {
//code to populate DataSet
chart = ChartFactory.createBarChart(
"Bar Chart", //Chart title
"", //Domain axis label
"MARKETS", //Range axis label
dataSet, //Chart Data
PlotOrientation.VERTICAL, // orientation
true, // include legend?
true, // include tooltips?
false // include URLs?
);
chart.setBorderVisible(true);
return SUCCESS;
}
}
Following is my struts.xml :
<action name="testAction"
class="testAction"
method="execute">
<result name="success" type="chart">
<param name="value">chart</param>
<param name="type">jpeg</param>
<param name="width">600</param>
<param name="height">400</param>
</result>
</action>
<action name="displayDataAction"
class="testAction"
method="getData">
<result name="success">test.jsp</result>
</action>
Jsp is :
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Data</title>
<s:head theme="ajax" debug="true"/>
</head>
<body bgcolor="white">
<s:form validate="true" target="_blank">
<table>
//Mapping of data from database
</table>
<s:submit id="submit" value="Display Chart" align="left" action="testAction"/>
<s:submit value="Display Data" align="left" action="displayDataAction"/>`
</s:form>
</body>
</html>
Instead of using s:form tag and s:submit, use form tag like:
<form action="blah blah" target="_blank">
And that should resolve the issue.

Different behavior on click of two buttons on same page in Struts 2

I have test.jsp which has two buttons.on click of Display Chart button,it should open chart on new page which is happening using target=_blank in form tag. But problem is i don't want this behavior on click of Display Data button. On click of this button,I want to display data fetched on same page only,but currently for this button as well it is opening a new window .
My code is:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Data</title>
<s:head theme="ajax" debug="true"/>
</head>
<body bgcolor="white">
<s:form validate="true" target="_blank">
<table>
//Mapping of data from database
</table>
<s:submit id="submit" value="Display Chart" align="left" action="testAction"/>
<s:submit value="Display Data" align="left" action="displayDataAction"/>`
</s:form>
</body>
</html>
public class TestAction extends ActionSupport{
public String execute() throws Exception {
//code to populate DataSet
chart = ChartFactory.createBarChart(
"Bar Chart", //Chart title
"", //Domain axis label
"MARKETS", //Range axis label
dataSet, //Chart Data
PlotOrientation.VERTICAL, // orientation
true, // include legend?
true, // include tooltips?
false // include URLs?
);
chart.setBorderVisible(true);
return SUCCESS;
}
}
struts.xml:
<action name="testAction"
class="testAction"
method="execute">
<result name="success" type="chart">
<param name="value">chart</param>
<param name="type">jpeg</param>
<param name="width">600</param>
<param name="height">400</param>
</result>
</action>
<action name="displayDataAction"
class="testAction"
method="getData">
<result name="success">test.jsp</result>
</action>
I got it done as follows :
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
function openPopUp(){
document.getElementById('testForm').setAttribute('target', '_blank');
document.getElementById('testForm').setAttribute('action', 'testAction.action');
document.getElementById('testForm').submit();
}
function noPopUp(){
document.getElementById('testForm').setAttribute('target', '');
}
<title>Data</title>
<s:head theme="ajax" debug="true"/>
</head>
<body bgcolor="white">
<s:form validate="true" target="_blank">
<table>
//Mapping of data from database
</table>
<s:submit id="submit" value="Display Chart" align="left" onclick="openPopUp();"/>
<s:submit value="Display Data" align="left" action="displayDataAction" onclick="noPopUp();"/>`
</s:form>
</body>
</html>

Indexed properties are getting duplicated struts2 on form submission

I need to add/edit/delete the table of objects in struts2. When I submit the form, I am getting objects duplicated.
Let me know where I made a mistake.
This is my code:
<s:form action="addPromotionLineItems" id="promotionLineItemsAddFormId">
<display:table id="data"
name="lstPromotionLineItems" sort="external" uid="row" htmlId="rowid"
class="tborder" excludedParams="*" style="width:100%" export="false">
<display:column titleKey="table.title.material" style="width:100px">
<s:property value="%{#attr.row.materialCode}" />
<s:hidden
name="lstPromotionLineItems(%{#attr.row_rowNum-1}).promotionLineItemId"
value="%{#attr.row.promotionLineItemId}" />
<s:hidden
name="lstPromotionLineItems(%{#attr.row_rowNum-1}).materialCode"
value="%{#attr.row.materialCode}" />
<s:hidden
name="lstPromotionLineItems(%{#attr.row_rowNum-1}).material.description"
value="%{#attr.row.material.description}" />
<s:hidden
name="lstPromotionLineItems(%{#attr.row_rowNum-1}).programId"
value="%{#attr.row.programId}" />
</display:column>
<display:column property="material.description"
titleKey="table.title.materialdesc" />
</s:form>
function refreshTableRecords(url,formNameId,resultId)
$.ajax({
type:'POST',
url: url,
data:$('#'+formNameId).serialize(),
success : function(response) {
$('#'+resultId).html(response);
},
error:function(data)
{
alert(data);
}
});
}
I have this content in jQuery ui modal popup.
I think when mapping to collection data in struts the syntax is not:
name="lstPromotionLineItems(%{#attr.row_rowNum-1}).promotionLineItemId"
rather:
name="lstPromotionLineItems[%{#attr.row_rowNum-1}].promotionLineItemId"
There is a slight difference in the bracketing around the row_num attribute in the input name.

Confused about how Silverlight gets onto the page

I have spent a while going over all of the examples on http://www.silverlight.net/learn/quickstarts/
And I'm still pretty lost about Silverlight. I'm not understanding exactly how it gets 'on' to the website. Like... is there any kind of tutorial that shows how you make an HTML webpage that retrieves a silverlight page and displays it, so you can work with it?
All I've had to work with so far are the default generated .aspx files, which don't really tell me much. And even using the default 'MVC Application' generator from Visual Studio is kind of cloudy and fuddled up.
I've done some google searches and glanced around at references, but maybe I'm just kind of dumb. I'm just not getting where it all 'comes together', so to speak. Any hints? Or am I just beyond learning?
The short answer is that you include Silverlight applications in a web page by using an object tag in HTML. The web browser is then responsible for loading the silverlight application similar to the way it loads Flash applications. Check out these quickstarts for a more detailed explanation. Don't be discouraged, this is a new concept to a lot of developers :).
Here is an example of a Silverlight application being embedded in a web page:
<body>
<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2,"
type="application/x-silverlight-2"
width="100%" height="100%">
<param name="source" value="HelloWorld.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="4.0.50401.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0"
style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376"
alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object>
<iframe id="_sl_historyFrame"
style="visibility:hidden;height:0px;width:0px;border:0px">
</iframe>
</div>
</form>
</body>
Here is the Microsoft tutorial on Embedding Silverlight into HTML. The .xap is the equivalent to Jnlp for modern applets and .swf for Flash. The parameters can control the code and can also be updated via Javascript. The silverlight plugin within the browser executes the application code. HTML just holds it in place.
Just create a Silverlight Application and choose for it to create aHost the Silverlight Application in a Web site.
It will create a project that generates the page that contains silverlight object that loads the xap file on the client.
The xap file is located in the ClientBin, the xap file is the Silverlight application.
As long as you point the object source <param> to the path of the xap file, it should load the respective Silverlight App.
The project it creates:
Example aspx page:
<%# Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>SilverlightApplication1</title>
<style type="text/css">
html, body {
height: 100%;
overflow: auto;
}
body {
padding: 0;
margin: 0;
}
#silverlightControlHost {
height: 100%;
text-align:center;
}
</style>
<script type="text/javascript" src="Silverlight.js"></script>
<script type="text/javascript">
function onSilverlightError(sender, args) {
var appSource = "";
if (sender != null && sender != 0) {
appSource = sender.getHost().Source;
}
var errorType = args.ErrorType;
var iErrorCode = args.ErrorCode;
if (errorType == "ImageError" || errorType == "MediaError") {
return;
}
var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n" ;
errMsg += "Code: "+ iErrorCode + " \n";
errMsg += "Category: " + errorType + " \n";
errMsg += "Message: " + args.ErrorMessage + " \n";
if (errorType == "ParserError") {
errMsg += "File: " + args.xamlFile + " \n";
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
else if (errorType == "RuntimeError") {
if (args.lineNumber != 0) {
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
errMsg += "MethodName: " + args.methodName + " \n";
}
throw new Error(errMsg);
}
</script>
</head>
<body>
<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/SilverlightApplication1.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40818.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
</form>
</body>
</html>

Resources