load parts of a JSON in many divs + Struts2 - json

I need load the content of a Object JSON in many divs, but in parts. For example,
My JSON structure:
{"Example": {
"Hi": "hi",
"Bye": "bye"
}
}
Assuming that the JSON string successfully load my JSP page. I am trying to load the contents of the JSON like this:
(For the attribute Hi and Bye)
<sj:div id="div1" dataType = "json">
<s:property value="Example.Hi"/>
</sj:div>
<sj:div id="div2" dataType = "json">
<s:property value="Example.Bye"/>
</sj:div>
Struts.xml:
<action name="name" class="class" method="method">
<result type="json">
<param name="root">
Example
</param>
</result>
</action>
but this doesn't work... What I can do?
I'm using: Struts2 Jquery Library

You should build the URL like
<s:url var="remoteurl1" action="name"><s:param name="div1" value="true"/></s:url>
<sj:div id="div1" href="%{#remoteurl1}" dataType = "json"/>
<s:url var="remoteurl2" action="name"><s:param name="div2" value="true"/></s:url>
<sj:div id="div2" href="%{#remoteurl2}" dataType = "json"/>
Then in your action you check if isDiv1() or isDiv2() and return corresponding result.

Related

Hit query on pagination in display tag

I use DisplayTag in my struts2 application and i want to hit query on clicking pagination.
Ex : When user click on the next page or any page then query is fire on action class.
FILE : displayTag.jsp
<display:table name="list1" sort="list" size="20" pagesize="5" id="table1" export="true" requestURI="" partialList="true">
<display:column property="no" group="1" sortable="true" headerClass="sortable"></display:column>
<display:column property="nam" group="2" sortable="true" headerClass="sortable"></display:column>
<display:column property="ct" group="3" sortable="true" headerClass="sortable" autolink="true"></display:column>
<display:setProperty name="export.excel.filename" value="diplayTag.xls"></display:setProperty>
<display:setProperty name="export.pdf.filename" value="diplayTag.pdf"></display:setProperty>
<display:setProperty name="export.csv.filename" value="diplayTag.csv"></display:setProperty>
<display:setProperty name="export.pdf" value="true"></display:setProperty>
</display:table>
I use request.setAttribute("list1", li); where i set all data in list1(ArrayList) and pass to the displayTag.jsp.
DisplayTag get all data and display in the table format. But my need is to pass only 5 data at a time and on clicking next page action class send other 5 data and so on.
I refer link : Display tag pagination problem
But i can not understand because i'm use MySql and also new on DisaplyTag.
DB : MySql
Framework : struts2
After researching and hard working i found answer.
FILE : displayTag.jsp
<display:table name="list1" sort="external" size="20" pagesize="5" id="table1" export="true" requestURI="disTag" partialList="true">
// code as above
</dispaly:table>
here requestURI="disTag" is a action name.
FILE : struts.xml
<action name="disTag" class="className">
<result name="success">/displayTag.jsp</result>
<result name="error">/error.jsp</result>
</action>
FILE : class file
page = Integer.parseInt(request.getParameter((new ParamEncoder("table1").encodeParameterName(TableTagParameters.PARAMETER_PAGE))));
if(page != 0)
{
start = (page - 1) * 5; //5 is row or data per page.
}
getData(start, 5); //getData is a method which store all data in ArrayList. Based on start index.

How to skip CSV header line with camel-beanio

How to skip CSV header line when using camel-beanio from apache?
My XML file for mapping look like this:
<beanio>
<record name="myRecord" class="my.package.MyConditionClass">
<field name="myField" position="1" />
<field name="mylist" position="2" collection="list" type ="string"/>
<segment name="conditions" class="my.package.MyConditionClass" nillable="true" collection="map" key="myKey">
<field name="myKey" position="2">
<field name="myValue" position="3">
</segment>
</record>
</beanio>
But to make my code run i must delete the first line (header line) manually. How do skip the header line automatically?
To read a CSV file and ignore the first header line, you can define the first field value of the header as comments of the CSV Stream
Example of CSV :
toto;tata;titi
product1;1;18
product2;2;36
product3;5;102
The mapping file :
<beanio ...
<stream name="dataStream" format="csv" >
<parser>
<property name="delimiter" value=";" />
<!-- ignore header line -->
<property name="comments" value="toto" />
</parser>
<record name="record" minOccurs="0" maxOccurs="unbounded" class="com.stackoverflow.Product" />
</stream>
</beanio>
Source : http://beanio.org/2.0/docs/reference/index.html#CSVStreamFormat
Another way will be to use camel-bindy in place of camel-beanio and the new option skipFirstLine (see https://camel.apache.org/components/latest/bindy-dataformat.html#_1_csvrecord)
Shortcut:
As soon as define BeanReader to read/process the records, use it's skip method with count 1 to skip the header.
e.g.
// Define Reader to process records
BeanReader beanReader = factory.createReader("STREAM",inputStreamReader);
// Skip First Record
beanReader.skip(1);
// Process rest of Stream
Object record;
do {
try {
record = beanReader.read();
}
catch (BeanReaderException e) {
e.printStackTrace();
}
} while(record !=null)
Refer http://beanio.org/2.0/docs/reference/index.html#TheMappingFile.
Skip method signature:
public int skip(int count) throws BeanReaderException;

xml processing in mysql

I have an xml persisted in database something as follows:
<root available-locales="en_US" default-locale="en_US">
<dynamic-element name="fdemo-redirect-url" repeatable="false" dataType="string" indexType="" required="false" showLabel="true" type="text" width="25">
<meta-data locale="en_US">
<entry name="label"><![CDATA[fdemo-redirect-url]]></entry>
</meta-data>
</dynamic-element>
<dynamic-element name="close_button" repeatable="false" dataType="document-library" indexType="" required="false" showLabel="true" type="ddm-documentlibrary" fieldNamespace="ddm">
<meta-data locale="en_US">
<entry name="label"><![CDATA[close_button]]></entry>
</meta-data>
</dynamic-element>
....
I need to write a query which should replace an hyphen in name attribute of dynamic-element tag whose dataType attribute is html. I ended up creating the following query. But this works only for one tag at a time. I do not want to loop it using a procedure. Is there a way to tweak this query in such a way that this works for multiple dynamic-element tags?
UPDATE table_name
SET xsd = UpdateXML(
xsd,
'//dynamic-element[5]/#name',
CONCAT(
'name="',
REPLACE(ExtractValue(xsd, '//dynamic-element[5]/#name'), '-', '_'),
'"'))
WHERE ExtractValue(xsd, '//dynamic-element[5]/#dataType') = 'html';
Here in this above query only 5th dynamic-element tag is handled for all the rows.

Dynamically Creating list in Struts2 using tags

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(2012);
list.add(2013);
list.add(2014);
list.add(2015);
can we do same as above using struts2 tags. may be by using
<s:set name="myList" value={somedynamic values} />
actually I want to create a list of number of 10 years on JSP page using Struts2 tags.
Regarding to your question answer is yes,but that is not an good idea to create number of 10 years in jsp page.
However, this is using arrylist in dynamic way
     
<s:select label="Years" headerKey="-1" headerValue="Select Years" list="list" name="your desire name" />
in the place of list property you have to give arrayList variable in your case it is list
means,
<s:select ---- list="your array list variable" --------- />
     and you have to define this action name in struts.xml
   eg:
<action name="yourarrylistvariable" class="your class" method="your method">
<result name="success">your jsp page</result>
</action>
    
     This is using arrylist in static way here you have to change the list value 
<s:select label="Years" headerKey="-1" headerValue="Select Years"
list="#{'2000':'2000', '2013':'2013',.....}" name="your desire name" />
For More Info You can refer this link struts2 select
Sure you can, thanks to the OGNL you can create lists like so:
<s:set var="myList" value="{2012,2013,2014,2015}" />
See this link.

Struts ajax form validation

i have a problem with ajax form validation. I'm using struts-core 2.3.1.2, struts-jquery-plugin 3.3.0 and struts-json-plugin.
The problem occurs if an ajax form will be submitted by an ajax request and validation fails. Cause then the whole form will be placed on the result element. Therefore you can activate ajax validation on the ajax sumbit button.
http://code.google.com/p/struts2-jquery/wiki/Validation
Here are also outdated information:
http://struts.apache.org/2.2.3.1/docs/ajax-validation.html
But the interceptor "jsonValidationWorkflowStack" is missing in struts-default.xml like written in post: jsonValidationWorkflowStack seems to be removed in Struts 2.3.1
It is sourced out to the struts-json-plugin in struts-plugin.xml. I don't know how i can use this directly but i build my own Stack in struts.xml:
<!-- Sample JSON validation stack -->
<interceptor-stack name="jsonValidationWorkflowStack">
<interceptor-ref name="basicStack"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel</param>
</interceptor-ref>
<interceptor-ref name="jsonValidation"/>
<interceptor-ref name="workflow"/>
</interceptor-stack>
</interceptors>
<action name="updateMySettings" method="execute" class="de.ra.daod.actions.MyAppSettingAction">
<interceptor-ref name="jsonValidationWorkflowStack"/>
<!-- This is not beauty within ajax -->
<result name="input">/WEB-INF/jsp/mysetting_ajax.jsp</result>
<result name="success" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
And my form looks like:
<s:head />
<sj:head />
<!-- This files are needed for AJAX Validation of XHTML Forms -->
<script src="${pageContext.request.contextPath}/struts/xhtml/validation.js" type="text/javascript"></script>
<s:form id="form" action="private/updateMySettings" theme="xhtml">
<s:textfield id="screenRes" key="appSetting.screenResolution" label="Screen resolution" required="true" />
<s:select key="appSetting.screenDepth" label="Color depth" list="#{'8':'8','16':'16','24':'24'}" required="true" />
<sj:submit value="Update Settings" targets="status" validate="true"/>
</s:form>
Unfortunately i get a javascript error if validation fails or not:
Uncaught TypeError: Object #<Object> has no method 'indexOf'
f.extend.ajax jquery-1.7.1.min.js:4
b.fn.ajaxSubmit
a.struts2_jquery.validateForm jquery.struts2-3.3.0.min.js:18
a.subscribeHandler.h.beforeSubmit jquery.struts2-3.3.0.min.js:18
b.fn.ajaxSubmit
a.subscribeHandler.e jquery.struts2-3.3.0.min.js:18
e.extend.each jquery-1.7.1.min.js:2
a.subscribeHandler.e jquery.struts2-3.3.0.min.js:18
f.event.dispatch jquery-1.7.1.min.js:3
f.event.add.h.handle.i jquery-1.7.1.min.js:3
f.event.trigger jquery-1.7.1.min.js:3
f.fn.extend.trigger jquery-1.7.1.min.js:3
e.extend.each jquery-1.7.1.min.js:2
e.fn.e.each jquery-1.7.1.min.js:2
f.fn.extend.trigger jquery-1.7.1.min.js:3
d.fn.extend.publish jquery.subscribe.min.js:16
e.extend.each jquery-1.7.1.min.js:2
d.fn.extend.publish jquery.subscribe.min.js:16
(anonymous function) jquery.struts2-3.3.0.min.js:18
f.event.dispatch jquery-1.7.1.min.js:3
f.event.add.h.handle.i jquery-1.7.1.min.js:3
It seems that the json object from the response can't be handled and i don't know why cause i followed the old instructions. I assume the cause is the function StrutsUtils.getValidationErrors from struts/utils.js if this function is used with the json object but i'm not sure. Can anyone help ?
I guess stream result and ajax doesn't play well together. just remove the targets
attribute. – jogep
I don't agree with you because the tenor of AJAX isn't load a new page but rather load pieces of whatever to the current page that is why i use an AJAX submit button. To notice the user of the action without reloading the page itself. The workaround is to clear the content of the status div element with javascript. I think that can be automated in struts2-jquery-plugin. This is my form which is embedded in tabbed pane.
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
<s:if test="appSetting != null">
<h3>Application settings</h3>
<div id="status" class="welcome"></div>
<table>
<tr>
<td>
<s:form id="form" action="updateMySettings" theme="xhtml">
<s:textfield id="screenRes" key="appSetting.screenResolution"
label="Screen resolution" required="true" />
<s:select key="appSetting.screenDepth" label="Color depth"
list="#{'8':'8','16':'16','24':'24'}" required="true" />
<sj:submit id="updateSetting" value="Update Settings" targets="status" validate="true" />
</s:form>
</td>
<td valign="top">
<button id="fullScreen">Use full screen</button>
<button id="fullBrowser">Use full browser</button>
</td>
</tr>
</table>
<script>
$("#fullScreen").click(function() {
scr_width = Math.max(screen.width,screen.height);
scr_height = Math.min(screen.width,screen.height);
$("#screenRes").val(scr_width + 'x' + scr_height);
});
$("#fullBrowser").click(function() {
brw_width = Math.max(window.innerWidth, window.innerHeight);
brw_height = Math.min(window.innerWidth, window.innerHeight);
$("#screenRes").val(brw_width + 'x' + brw_height);
});
$("#updateSetting").click(function() {
$("#status").empty();
})
</script>
</s:if>
<s:else>
<p>No settings available.</p>
</s:else>
It works very well.
You are step in an solved Struts2 Issue. Upgrade to latest version 2.3.3 or 2.3.4 and your problem should be gone.