Displaying JSON data in dropdown list in struts2 - ajax

I was doing good with my code. But i am struck at one problem. I want to populate the json data in my select box in struts2.
What i am doing is..i am sending the ajax request on the click of button and displaying the form.
Form is initially has property display none. when i click on the button it changes to block.
But there is one error coming whenever i am opening my jsp page into the browser.
The requested list key 'categories' could not be resolved as a
collection
my 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.custom.i18n.resources" value="LoginAction" />
<package name="default" extends="struts-default" namespace="/">
<action name="Login" class="com.agents.cb.LoginAction" >
<result name="success" type="redirect">Welcome.jsp</result>
<result name="input">login.jsp</result>
</action>
<action name="Register" method="register" class="com.agents.cb.LoginAction" >
<result name="success">login.jsp</result>
</action>
</package>
<package name="admin" extends="json-default" namespace="/admin">
<action name="addCategory" method="addCategory" class="com.type.user.Admin" >
<result type="json">
<param name="excludeNullProperties">true</param>
<param name="excludeProperties">
catName,catCode
</param>
</result>
</action>
<action name="categories" method="categories" class="com.type.user.Admin" >
<result type="json">
<param name="excludeNullProperties">true</param>
<param name="excludeProperties">
catName,catCode,status
</param>
</result>
</action>
</package> </struts>
Admin.java file
package com.type.user;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import com.type.product.*;
public class Admin extends ActionSupport {
private String catCode;
private String catName;
private HttpServletRequest request;
private Map<String, String> categories = new HashMap<String, String>();;
private String status;
public Map<String, String> getCategories() {
return categories;
}
public void setCategories(Map<String, String> categories) {
this.categories = categories;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public String getCatCode() {
return catCode;
}
public void setCatCode(String catCode) {
this.catCode = catCode;
}
public String getCatName() {
return catName;
}
public void setCatName(String catName) {
this.catName = catName;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String addCategory() {
Product prod = new Product();
boolean flag = prod.addCat(catCode, catName);
//boolean flag=false;
if (flag) {
status=catName+" added successfully.";
return SUCCESS;
} else {
status="Error occurred while adding "+catName;
return SUCCESS;
}
}
public String categories(){
System.out.println("step 1");
Product prod=new Product();
categories=prod.getAllCategories();
return SUCCESS;
}
}
my script on jsp page
$(".add_sub_category").click(function(e){
e.preventDefault();
$.ajax({
url : '/Login/admin/categories',
type : 'POST',
dataType : 'json',
sucess : function(resp) {
console.log(resp);
}
});
});
Please help. Where i am doing wrong? and why i have this problem.

You declared this
<package name="admin" extends="json-default" namespace="/admin">
<action name="categories" method="categories" class="com.type.user.Admin" >
but you are trying to call it at
url : '/Login/admin/categories',
change it to the path you declared:
url : '/admin/categories',
because Login is not defined anywhere as part of the namespace.
Also pay attention when playing with different namespaces...
Hope that helps

Related

TableView not showing items [duplicate]

OK, new to java by several weeks, but have been programming for 30 years. The following code executes, but only the first column is showing anything. The data object is showing multiple rows of data, with fields of data that are filled in. I'm sure I'm missing something, and have looked through similar questions on here.
APVoucher_batchgridController.java
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;
import javafx.scene.input.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.PropertyValueFactory;
/**
* FXML Controller class
*
* #author kmitchell
*/
public class APVoucher_batchgridController implements Initializable {
public TableView tblMainList;
public TableColumn colDateEntered;
public TableColumn colCreatedBy;
public TableColumn colDescription;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
}
#FXML
public void opentables(ActionEvent event) {
Object forName = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
colDateEntered.setCellValueFactory(new PropertyValueFactory<sresult, String>("DateEntered"));
colDescription.setCellValueFactory(new PropertyValueFactory<sresult, String>("cDesc"));
colCreatedBy.setCellValueFactory(new PropertyValueFactory<sresult, String>("CreatedBy"));
try {
// load the driver into memory
forName = Class.forName("jstels.jdbc.dbf.DBFDriver2");
} catch (ClassNotFoundException ex) {
Logger.getLogger(APVoucher_batchgridController.class.getName()).log(Level.SEVERE, null, ex);
}
try {
conn = DriverManager.getConnection("jdbc:jstels:dbf:e:\\keystone-data\\keyfund\\seymour\\keyfund.dbc");
} catch (SQLException ex) {
Logger.getLogger(APVoucher_batchgridController.class.getName()).log(Level.SEVERE, null, ex);
}
if (conn != null) {
try {
stmt = conn.createStatement();
} catch (SQLException ex) {
Logger.getLogger(APVoucher_batchgridController.class.getName()).log(Level.SEVERE, null, ex);
}
if (stmt != null) {
// execute a query
try {
ObservableList<Object> data = FXCollections.observableArrayList();
rs = stmt.executeQuery("SELECT denteredon, cdesc, ccreatedby FROM apvbatch WHERE ldeleted = false ORDER BY denteredon DESC");
while (rs.next()) {
String enteredon = rs.getString("denteredon");
String desc = rs.getString("cdesc");
String createdby = rs.getString("ccreatedby");
sresult row = new sresult(createdby, enteredon, desc);
data.add(row);
}
tblMainList.setItems(data);
tblMainList.setVisible(true);
} catch (SQLException ex) {
Logger.getLogger(APVoucher_batchgridController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public class sresult {
private String DateEntered;
private String EnteredBy;
private String cDesc;
public sresult(String T, String d, String c) {
this.EnteredBy = T;
this.DateEntered = d;
this.cDesc = c;
}
public String getEnteredBy() {
return EnteredBy;
}
public void setEnteredBy(String T) {
EnteredBy = T;
}
public String getDateEntered() {
return DateEntered;
}
public void setDateEntered(String d) {
DateEntered = d;
}
public String getcDesc() {
return cDesc;
}
public void setcDesc(String c) {
cDesc = c;
}
}
}
and APVoucher_batchgrid.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" fx:id="batchlistform" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="keystone.APVoucher_batchgridController">
<children>
<BorderPane layoutX="0.0" layoutY="0.0" prefHeight="400.0" prefWidth="600.0">
<center>
<AnchorPane prefHeight="-1.0" prefWidth="-1.0">
<children>
<Pane layoutX="0.0" layoutY="0.0" prefHeight="53.0" prefWidth="580.0">
<children>
<Label layoutX="7.0" layoutY="9.0" prefWidth="202.0" text="AP Vouchers Batch List">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Button fx:id="btnClose" cancelButton="true" layoutX="513.0" layoutY="27.0" mnemonicParsing="false" text="Close" />
<Button id="btnClose" fx:id="apvRefresh" cancelButton="true" layoutX="185.0" layoutY="27.0" mnemonicParsing="false" onAction="#opentables" text="Refresh" />
</children>
</Pane>
<TableView fx:id="tblMainList" layoutX="0.0" layoutY="53.0" prefHeight="323.0" prefWidth="580.0">
<columns>
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="91.0" text="Date Entered" fx:id="colDateEntered" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="100.0" text="Created By" fx:id="colCreatedBy" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="261.0" text="Description" fx:id="colDescription" />
</columns>
</TableView>
</children>
</AnchorPane>
</center>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</BorderPane>
</children>
<stylesheets>
<URL value="#apvoucher_batchgrid.css" />
</stylesheets>
</AnchorPane>
THANK YOU for the answer. Way to many years in case insensitive languages. This has been a quick and dirty exercise for me to learn java and the latest & greatest stuff or as I like to say New Exciting Technology (NExT!)
For anyone looking at the answer and still not completely clued in, here are the changes that made the code work properly.
colDateEntered.setCellValueFactory(new PropertyValueFactory<sresult, String>("Denteredon"));
colDescription.setCellValueFactory(new PropertyValueFactory<sresult, String>("CDesc"));
colEnteredBy.setCellValueFactory(new PropertyValueFactory<sresult, String>("Ccreatedby"));
public class sresult {
private String Denteredon;
private String Ccreatedby;
private String CDesc;
public sresult(String T, String d, String c) {
this.Ccreatedby = T;
this.Denteredon = d;
this.CDesc = c;
}
public String getCcreatedby() {
return Ccreatedby;
}
public void setCreatedby(String T) {
Ccreatedby = T;
}
public String getDenteredon() {
return Denteredon;
}
public void setDenteredon(String d) {
Denteredon = d;
}
public String getCDesc() {
return CDesc;
}
public void setCDesc(String c) {
CDesc = c;
}
}
}
This question is really a duplicate of: Javafx PropertyValueFactory not populating Tableview, but I'll specifically address your specific case, so it's clear.
Suggested solution (use a Lambda, not a PropertyValueFactory)
Instead of:
aColumn.setCellValueFactory(new PropertyValueFactory<Appointment,LocalDate>("date"));
Write:
aColumn.setCellValueFactory(cellData -> cellData.getValue().dateProperty());
For more information, see this answer:
Java: setCellValuefactory; Lambda vs. PropertyValueFactory; advantages/disadvantages
How do you use a JavaFX TableView with java records?
demonstrates replacing PropertyValueFactory with lambda expressions.
Solution using PropertyValueFactory
The lambda solution outlined above is preferred, but if you wish to use PropertyValueFactory, this alternate solution provides information on that.
Background
PropertyValueFactory uses reflection to determine the methods to get and set data values as well as to retrieve bindable properties from your model class. The pattern followed is:
PropertyValueType getName()
void setName(PropertyValueType value)
PropertyType nameProperty()
Where "name" is the string specified in the PropertyValueFactory constructor. The first letter of the property name in the getter and setter is capitalized (by java bean naming convention).
Why your application doesn't work
You have these three expressions:
new PropertyValueFactory<sresult, String>("DateEntered")
new PropertyValueFactory<sresult, String>("cDesc")
new PropertyValueFactory<sresult, String>("CreatedBy")
For your sample properties, the PropertyValueFactory will look for these methods:
"DateEntered" => getDateEntered()
"cDesc" => getCDesc()
"CreatedBy" => getCreatedBy()
And you have these three getters on your sresult class:
getDateEntered()
getcDesc()
getEnteredBy()
Only getDateEntered() is going to be picked up by the PropertyValueFactory because that is the only matching method defined in the sresult class.
Advice
You will have to adopt Java standards if you want the reflection in PropertyValueFactory to work (the alternative is to not use the PropertyValueFactory and instead write your own cell factories from scratch).
Adopting Java camel case naming conventions also makes it easier for Java developers to read your code.
Some times columns doesn't show data because of column names. eg,
new PropertyValueFactory<sresult, String>("cDesc")
and getter is getcDesc cDesc column may not display data. If you change code to
new PropertyValueFactory<sresult, String>("CDesc")
and getter is getCDesc CDesc column may display data.
For anyone else who still wasn't getting it after going through the above, my problem was that I wasn't specifying my setters with the "public final" designation.

Wearable.MessageApi.sendMessage returning Success but onMessageReceived is not calling

Working on Wear application, I have created Wear application with Mobile and wear applications. Sending data from mobile application, through "MessageApi.SendMessageResult" and returning status is SUCCESS but message is not received in wear application. Please find the code in below and let me know i am missing any thing.
Mobile App Code:
public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks {
private static final String START_ACTIVITY = "/start_activity";
private static final String WEAR_MESSAGE_PATH = "/message";
private GoogleApiClient mApiClient;
private ArrayAdapter<String> mAdapter;
private ListView mListView;
private EditText mEditText;
private Button mSendButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initGoogleApiClient();
}
private void initGoogleApiClient() {
mApiClient = new GoogleApiClient.Builder( this )
.addApi( Wearable.API )
.build();
mApiClient.connect();
}
#Override
protected void onDestroy() {
super.onDestroy();
mApiClient.disconnect();
}
private void init() {
mListView = (ListView) findViewById( R.id.list_view );
mEditText = (EditText) findViewById( R.id.input );
mSendButton = (Button) findViewById( R.id.btn_send );
mAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1 );
mListView.setAdapter( mAdapter );
mSendButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = mEditText.getText().toString();
if (!TextUtils.isEmpty(text)) {
mAdapter.add(text);
mAdapter.notifyDataSetChanged();
sendMessage(WEAR_MESSAGE_PATH, text);
}
}
});
}
private void sendMessage( final String path, final String text ) {
new Thread( new Runnable() {
#Override
public void run() {
//Previous code
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes( mApiClient ).await();
Log.d("MessageAPI","nodes :: "+nodes);
for(com.google.android.gms.wearable.Node node : nodes.getNodes()) {
Log.d("MessageAPI","nodes for :: "+node);
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
mApiClient, node.getId(), path, text.getBytes() ).await();
Log.d("MessageAPI","node.getId() : "+node.getId());
Log.d("MessageAPI","text.getBytes() : "+text.getBytes());
Log.d("MessageAPI","path : "+path);
Log.d("MessageAPI","nodes result Status:: "+result.getStatus().isSuccess());
}
/*PutDataMapRequest putDMR = PutDataMapRequest.create(path);
putDMR.getDataMap().putAll(getDatMap());
PutDataRequest request = putDMR.asPutDataRequest();
DataApi.DataItemResult result = Wearable.DataApi.putDataItem(mApiClient, request).await();
if (result.getStatus().isSuccess()) {
Log.v("MessageAPI", "nodes DataMap: " + getDatMap() + " sent successfully to data layer ");
} else {
// Log an error
Log.v("MessageAPI", "nodes ERROR: failed to send DataMap to data layer");
}*/
runOnUiThread( new Runnable() {
#Override
public void run() {
mEditText.setText( "" );
}
});
}
}).start();
}
#Override
public void onConnected(Bundle bundle) {
sendMessage(START_ACTIVITY, "Wear my TEST MESSAGE");
}
#Override
public void onConnectionSuspended(int i) {
}
private DataMap getDatMap(){
DataMap dataMap = new DataMap();
dataMap.putLong("time", new Date().getTime());
dataMap.putString("hole", "1");
dataMap.putString("front", "250");
dataMap.putString("middle", "260");
dataMap.putString("back", "270");
return dataMap;
}
}
Mobile App manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ptrprograms.wearmessageapi" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
wearApp Code
public class WearMessageListenerService extends WearableListenerService {
private static final String START_ACTIVITY = "/start_activity";
#Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.d("MessageAPI","onMessageReceived :: "+ messageEvent.getPath());
/* if( messageEvent.getPath().equalsIgnoreCase( START_ACTIVITY ) ) {
Intent intent = new Intent( this, MainActivity.class );
intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity( intent );
} else {
super.onMessageReceived(messageEvent);
}*/
showToast("onMessageReceived:: "+messageEvent.getPath());
Intent intent = new Intent( this, MainActivity.class );
intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity( intent );
}
private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
#Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
super.onDataChanged(dataEventBuffer);
Log.d("MessageAPI","onMessageReceived : onDataChanged: ");
}
}
wearApp manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-feature android:name="android.hardware.type.watch" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.DeviceDefault" >
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- <service android:name=".WearMessageListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>-->
<service android:name=".WearMessageListenerService">
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
<data android:scheme="wear" android:host="*" android:pathPrefix="/prefix" />
<!--<data android:scheme="wear" android:host="*"
android:path="/start_activity" />-->
<!--<action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
<action android:name="com.google.android.gms.wearable.CHANNEL_EVENT" />-->
<!-- <data android:scheme="wear" android:host="*" android:path="/start_activity" />-->
</service>
</application>
1) Problem is in wearApp in WearMessageListenerService. You don't call
super.onMessageReceived(messageEvent);
i.e you consume all messages. When you call super method you pass message to Wearable.MessageApi.addListener(...
2) I don't see in your wear app that you register MessageApi listener.
3) You mix MessageApi with Data Layer api.
4) In wear app in Main activity you need connect with google play services too. To register MessageApi listener in onConnected method.
5) Please check https://github.com/mariopce/android-wear-bilateral-communication.
This is example of using MessageApi for 2-way communication (mobile-wear-mobile)

Obfuscar SkipType configuration element is not working for enums

The Obfuscar SkipType configuration element seems to be not working for enums. This is my fairly minimal configuration file.
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0"
sku=".NETFramework,Version=v4.0,Profile=Client"/>
</startup>
<Obfuscator>
<Var name="InPath"
value="\users\user\docs\vs2013\projects\wpfapp\wpfapp\bin\debug" />
<Var name="OutPath"
value="\users\user\docs\vs2013\projects\wpfapp\wpfapp\bin\debug" />
<Module file="$(InPath)\wpfapp.exe" />
<Var name="KeepPublicApi" value="true" />
<Var name="HidePrivateApi" value="true" />
<SkipType name="WpfApp.Category" skipFields="true" skipProperties="true" />
</Obfuscator>
</configuration>
The map output file shows that the skipping did not work and the enum type Category was renamed.
Renamed Types:
[WpfApp]WpfApp.Category -> [WpfApp]A.a
{
WpfApp.Category [WpfApp]WpfApp.Category WpfApp.Category::Low -> A
WpfApp.Category [WpfApp]WpfApp.Category WpfApp.Category::High -> a
System.Int32 [WpfApp]System.Int32 WpfApp.Category::value__ skipped: special name
}
Edit: The element <SkipType name="WpfApp.Category" /> causes the same problem.
Edit: The element <SkipType name="WpfApp.Category" skipFields="true" /> causes the same problem.
Edit: The element <SkipField type="WpfApp.Category" name="*" /> causes the same problem.
Edit: This pair
<SkipField type="WpfApp.Category" name="Low" />
<SkipField type="WpfApp.Category" name="High" /> causes the same problem.
The source:
namespace WpfApp
{
public enum Category { Low, High }
//[System.Reflection.Obfuscation]
public partial class MainWindow : Window
{
private ViewModel ViewModel;
public MainWindow()
{
InitializeComponent();
this.DataContext = this.ViewModel = new ViewModel();
}
private void MyButtonClick(object sender, RoutedEventArgs e)
{
this.ViewModel.Process(MyTextBox.Text);
}
}
internal class ViewModel : WpfNotifier
{
private const float DefaultKilograms = 80.0f;
private string _kilograms;
public string Kilograms // WPF binds here
{
get { return this._kilograms; }
set { this._kilograms = value; NotifyPropertyChanged(); }
}
private string _resultText;
public string ResultText // WPF binds here
{
get { return this._resultText; }
set { this._resultText = value; NotifyPropertyChanged(); }
}
internal void Process(string input)
{
float kilograms;
if (Single.TryParse(input, out kilograms))
{
Category c = (kilograms > 100.0f) ? Category.High : Category.Low;
this.ResultText = c.ToString();
}
else
{
this.Kilograms = ViewModel.DefaultKilograms.ToString();
}
}
}
public class WpfNotifier : INotifyPropertyChanged
{
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged; // public for interface
internal void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
else
; // it is harmless to fail to notify before the window has been loaded and rendered
}
}
}
Is this a bug or is my usage wrong?
Your usage is wrong. If you check the documentation you will see that <SkipType> tags must be put into <Module> tags. Otherwise, Obfuscar has no idea in which module/assembly this skip rule takes effect. So you should try
<Module file="$(InPath)\wpfapp.exe">
<SkipType name="WpfApp.Category" skipFields="true" skipProperties="true" />
</Module>

EclipseLink MOXy: Binding working and not working with same XPath

Input:
<?xml version="1.0" encoding="UTF-8"?>
<foo:root xmlns:foo="http://www.domain.org/foo"
xmlns="http://www.domain.org/foo"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<a xsi:type="foo:someType">
<b text="some text" />
</a>
</foo:root>
Bindings:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="test">
<xml-schema element-form-default="QUALIFIED" namespace="http://www.domain.org/foo">
<xml-ns prefix="foo" namespace-uri="http://www.domain.org/foo" />
</xml-schema>
<java-types>
<java-type name="Root">
<xml-root-element name="root"/>
<java-attributes>
<xml-element java-attribute="contentRoot" xml-path="." type="test.ContentRoot" />
</java-attributes>
</java-type>
<java-type name="ContentRoot">
<java-attributes>
<xml-element java-attribute="text" xml-path="a/b/#text" />
<xml-element java-attribute="contents" xml-path="a/b" type="test.Content" container-type="java.util.List" />
</java-attributes>
</java-type>
<java-type name="Content">
<java-attributes>
<xml-element java-attribute="text" xml-path="#text" />
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Classes in package test:
public class Root {
private List<ContentRoot> contentRoots = new LinkedList<ContentRoot>();
public List<ContentRoot> getContentRoots() {
return contentRoots;
}
public void setContentRoots(List<ContentRoot> contentRoots) {
this.contentRoots = contentRoots;
}
public void setContentRoot(ContentRoot contentRoot) {
this.contentRoots.add(contentRoot);
}
}
public class ContentRoot {
private List<Content> contents;
private String text;
public List<Content> getContents() {
return contents;
}
public void setContents(List<Content> contents) {
this.contents = contents;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
public class Content {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Running Code:
Map<String, Object> jaxbContextProperties = new HashMap<String, Object>(1);
jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "bindings.xml");
JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {Root.class}, jaxbContextProperties);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Root root = (Root)unmarshaller.unmarshal(new File("input.xml"));
System.out.println(root.getContentRoots().get(0).getText());
System.out.println(root.getContentRoots().get(0).getContents().get(0).getText());
The result is, that text is set in ContentRoot but not in Content (I get a NullPointerException from the last System.out.println()). Can somebody tell me why?
There are a few items that are tripping you up:
ISSUE #1 - ContentRoot Contains Invalid Mappings
EclipseLink JAXB (MOXy) does not allow the following combination of mappings. With the second xml-element you are telling MOXy to map the contents properties to the repeating element b that occur within the element a. With the first xml-element you are trying to map the text attribute of one of the potentially many b elements to the String property text.
<java-type name="ContentRoot">
<java-attributes>
<xml-element java-attribute="text" xml-path="a/b/#text" />
<xml-element java-attribute="contents" xml-path="a/b" type="test.Content" container-type="java.util.List" />
</java-attributes>
</java-type>
The correct place to map the text attribute is on the Content class which you already had. Instead of using the xml-element mapping with the xml-path pointing at the attribute (which would work), I would recomment using the xml-attribute mapping and specifying a name.
<java-type name="Content">
<java-attributes>
<xml-attribute java-attribute="text"/>
</java-attributes>
</java-type>
ISSUE #2 - xml-path Is Not Properly Namespace Qualified
Since in bindings.xml you have associated the foo prefix with the http://www.domain.org/foo namespace URI.
<xml-schema element-form-default="QUALIFIED" namespace="http://www.domain.org/foo">
<xml-ns prefix="foo" namespace-uri="http://www.domain.org/foo" />
</xml-schema>
When you specify the xml-path you need to include the prefix in order to get the correct namespace qualification.
<java-type name="ContentRoot">
<java-attributes>
<xml-element java-attribute="contents" xml-path="foo:a/foo:b"/>
</java-attributes>
</java-type>
For More Information
http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html
Alternatively you could have mapped it with an xml-element-wrapper as follows:
<java-type name="ContentRoot">
<java-attributes>
<xml-element java-attribute="contents" name="b">
<xml-element-wrapper name="a"/>
</xml-element>
</java-attributes>
</java-type>
ISSUE #3 - xml-path="." Can Not Be Used on a Collection Property
Currently MOXy requires that each item in a collection correspond to its own element. This means that at this time you can not specify the self XPath . for collection properties.
Full Example
Your demo code didn't seem to fully match your domain model. Here is a complete example that pulls everything together:
Root
package test;
public class Root {
private ContentRoot contentRoot;
public ContentRoot getContentRoot() {
return contentRoot;
}
public void setContentRoot(ContentRoot contentRoot) {
this.contentRoot = contentRoot;
}
}
ContentRoot
package test;
import java.util.List;
public class ContentRoot {
private List<Content> contents;
public List<Content> getContents() {
return contents;
}
public void setContents(List<Content> contents) {
this.contents = contents;
}
}
Content
package test;
public class Content {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
bindings.xml
<?xml version="1.0"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="test"
xml-accessor-type="FIELD">
<xml-schema element-form-default="QUALIFIED" namespace="http://www.domain.org/foo">
<xml-ns prefix="foo" namespace-uri="http://www.domain.org/foo" />
</xml-schema>
<java-types>
<java-type name="Root">
<xml-root-element/>
<java-attributes>
<xml-element java-attribute="contentRoot" xml-path="."/>
</java-attributes>
</java-type>
<java-type name="ContentRoot">
<java-attributes>
<xml-element java-attribute="contents" xml-path="foo:a/foo:b"/>
</java-attributes>
</java-type>
<java-type name="Content">
<java-attributes>
<xml-attribute java-attribute="text"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Demo
package test;
import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> jaxbContextProperties = new HashMap<String, Object>(1);
jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "test/bindings.xml");
JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {Root.class}, jaxbContextProperties);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Root root = (Root)unmarshaller.unmarshal(new File("src/test/input.xml"));
System.out.println(root.getContentRoot().getContents().get(0).getText());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}

Strut2 execAndWait interceptor + JSON result type + Dojo dijit.ProgressBar = AJAX Progress Bar

I am using the Dojo ProgressBar to show a long running process in Struts2 using the execAndWait interceptor. The execAndWait interceptor puts the action on the value stack for each call that returns the wait result. However, when the result type is JSON, the action only has the default values of the action.
Here is my struts action config
(I tried to wild card the result names, but that didn't work)
NOTE: I have to wrap my JSON in a textarea because Dojo's iframe.send expects it to be wrapped in a textarea.
<action name="upload" class="ProcessFileAction" method="upload">
<interceptor-ref name="agfStack" />
<interceptor-ref name="execAndWait">
<param name="delay">1000</param>
<param name="delaySleepInterval">500</param>
</interceptor-ref>
<result name="wait" type="json">
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<param name="wrapPrefix"><![CDATA[<html><body><textarea>]]></param>
<param name="wrapSuffix"><![CDATA[</textarea></body></html>]]></param>
<param name="includeProperties">percentComplete,processMessage,running</param>
</result>
<result name="success" type="json">
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<param name="wrapPrefix"><![CDATA[<html><body><textarea>]]></param>
<param name="wrapSuffix"><![CDATA[</textarea></body></html>]]></param>
<param name="includeProperties">percentComplete,processMessage,running</param>
</result>
<result name="error" type="json">
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<param name="wrapPrefix"><![CDATA[<html><body><textarea>]]></param>
<param name="wrapSuffix"><![CDATA[</textarea></body></html>]]></param>
<param name="includeProperties">percentComplete,processMessage,running</param>
</result>
</action>
Here is my JSP
NOTE: I have to use the iFrame.send action beacuse I am uploading a file. Since my struts result will be the same for the initial wait return as it will for each successive call, I have to use the iframe.send for the AJAX call too (as opposed to xhrGet). That is because iframe.send expects the JSON to be wrapped in a text area, and xhrGet doesn't.
<%#taglib uri="http://www.springframework.org/security/tags" prefix="security"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<%#taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<script type="text/javascript">
dojo.require("dojo.io.iframe");
dojo.require("dijit.ProgressBar");
dojo.require('dojox.timing');
var t = new dojox.timing.Timer(<tiles:getAsString name="refreshTime" />);
t.onTick = function() {
dojo.io.iframe.send({
url: '<s:url action="upload" namespace="/" />',
method: "POST",
handleAs: "json",
load: function(response, ioArgs){
if(response.running) {
var progressPercent = response.percentComplete + "%";
var reportText = response.processMessage;
var uploadProgesssBar = dijit.byId("uploadProgress");
if(uploadProgesssBar == null) {
return;
}
uploadProgesssBar.update({progress:progressPercent, report:function() {return reportText;}});
}
else {
showById("progressDialogButtonDiv");
t.stop();
}
},
error: function(response, ioArgs) {
t.stop();
var progressPercent = response.percentComplete + "%";
var reportText = response.processMessage;
var uploadProgesssBar = dijit.byId("uploadProgress");
if(uploadProgesssBar == null) {
return;
}
uploadProgesssBar.update({progress:progressPercent, report:function() {return reportText;}});
showById("progressOkButton");
}
});
};
function showProgressBar() {
hideById("uploadForm");
showById("progressBar");
}
function hideProgressBar() {
hideById("progressBar");
showById("uploadForm");
}
function submitAndShowProgress(submitForm) {
dojo.io.iframe.send({
form: submitForm,
handleAs: "json",
load: function(response, ioArgs) {
submitForm.reset();
var progressPercent = response.percentComplete + "%";
var reportText = response.processMessage;
if(response.running) {
showProgressBar();
var uploadProgesssBar = dijit.byId("uploadProgress");
if(uploadProgesssBar == null) {
return;
}
uploadProgesssBar.update({progress:progressPercent, report:function() {return reportText;}});
t.start();
}
else {
createAndShowAlertDialog(reportText, "");
}
return response;
},
error: function(response, ioArgs) {
t.stop();
hideProgressBar();
createAndShowAlertDialog(response.processMessage, "An Error Occurred");
return response;
}
});
return false;
}
</script>
<div id="progressBar" class="hidden loading">
<div dojoType="dijit.ProgressBar" style="width:400px" jsId="uploadProgress" id="uploadProgress" annotate="true"></div>
<div class="hidden actionButtons" id="progressOkButton">
<button style="position: relative; left: 42px;" dojoType="dijit.form.Button" onClick="hideProgressBar(); return false;">Ok</button>
</div>
</div>
<s:form action="upload" namespace="/" enctype="multipart/form-data" id="uploadForm" method="post" onsubmit="return submitAndShowProgress(this);">
<s:file name="file" id="file" label="File" />
<s:submit id="submit" name="submit">Upload</s:submit>
</s:form>
Here is my action:
(trimmed)
public class ProcessFileAction implements LongRunning {
String processMessage = "Uploading...";
Integer percentComplete = 0;
Boolean running = true;
private FileProcessor fileProcessor;
private File file;
private String fileContentType;
private String fileFileName;
public String upload() throws Exception {
setProcessMessage("Processing File...");
setPercentComplete(10);
try {
if(!getFile().exists()) {
getFile().createNewFile();
}
File processedFile = getFileProcessor().process(getFile(), this);
} catch(Exception e) {
e.printStackTrace();
setProcessMessage("An error occurred while processing your file.");
setPercentComplete(100);
setRunning(false);
return ERROR;
}
setProcessMessage("Process Complete!");
setPercentComplete(100);
setRunning(false);
return SUCCESS;
}
...
}
public interface LongRunning {
public void setProcessMessage(String processMessage);
public String getProcessMessage();
public void setPercentComplete(Integer percentComplete);
public Integer getPercentComplete();
public void setRunning(boolean running);
public boolean isRunning();
}
I am going to look at the code for the JSON result type and ExecAndWait interceptor for more clues.
After looking at JSONResult, I found the problem.
When you set a 'root' object on the JSON, it looks for the values on the value stack. If not, it looks at the ActionInvocations action (which is not used with the ExecAndWait Intercepror).
public void execute(ActionInvocation invocation) throws Exception {
...
String json;
Object rootObject;
if (this.enableSMD) {
// generate SMD
rootObject = this.writeSMD(invocation);
} else {
// generate JSON
if (this.root != null) {
ValueStack stack = invocation.getStack();
rootObject = stack.findValue(this.root);
} else {
rootObject = invocation.getAction();
}
}
...
}
So, I created a LongRunningImpl class to hold the LongRunning information
public class LongRunningImpl implements LongRunning {
private Integer percentComplete = 0;
private String processMessage = "Uploading file...";
private boolean running = true;
...
}
I used that object in my action:
public class ProcessFileAction {
LongRunning longRunning;
private FileProcessor fileProcessor;
private File file;
private String fileContentType;
private String fileFileName;
public String upload() throws Exception {
getLongRunning().setProcessMessage("Processing File...");
getLongRunning().setPercentComplete(10);
try {
if(!getFile().exists()) {
getFile().createNewFile();
}
File processedFile = getFileProcessor().process(getFile(), getLongRunning());
} catch(Exception e) {
e.printStackTrace();
getLongRunning().setProcessMessage("An error occurred while processing your file.");
getLongRunning().setPercentComplete(100);
getLongRunning().setRunning(false);
return ERROR;
}
getLongRunning().setProcessMessage("Process Complete!");
getLongRunning().setPercentComplete(100);
getLongRunning().setRunning(false);
return SUCCESS;
}
...
}
And then set a root object on my results:
<action name="upload" class="ProcessFileAction" method="upload">
<interceptor-ref name="agfStack" />
<interceptor-ref name="execAndWait">
<param name="delay">1000</param>
<param name="delaySleepInterval">500</param>
</interceptor-ref>
<result name="wait" type="json">
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<param name="wrapPrefix"><![CDATA[<html><body><textarea>]]></param>
<param name="wrapSuffix"><![CDATA[</textarea></body></html>]]></param>
<param name="root">longRunning</param>
<param name="includeProperties">percentComplete,processMessage,running</param>
</result>
<result name="success" type="json">
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<param name="wrapPrefix"><![CDATA[<html><body><textarea>]]></param>
<param name="wrapSuffix"><![CDATA[</textarea></body></html>]]></param>
<param name="root">longRunning</param>
<param name="includeProperties">percentComplete,processMessage,running</param>
</result>
<result name="error" type="json">
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<param name="wrapPrefix"><![CDATA[<html><body><textarea>]]></param>
<param name="wrapSuffix"><![CDATA[</textarea></body></html>]]></param>
<param name="root">longRunning</param>
<param name="includeProperties">percentComplete,processMessage,running</param>
</result>
</action>
Now everything works! I might list this as a defect for Struts2, but I am not sure how it'll be received. I think the JSON Result object should check for the action on the value stack before defaulting back to the action on the ActionInvocation.

Resources