Error: Could not find action or result No result defined for action action.Part and result { col1 : col1 , col2 : col2 }
A dataType : 'json'
is used by jQuery Ajax to specify a data type that is expected to return by the success
callback function when the action and result is executed, and a response returned from the server.
dataType
(default: Intelligent Guess (xml
,json
,script
, orhtml
))Type:
String
The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).
The URL should correctly point to the action mapping. Assume it will be in the default namespace, otherwise you should modify URL and mapping to add the namespace
attribute.
<script type="text/javascript">
$(function() {
$("#dialog-form").dialog ({
autoOpen: true,
height: 500,
width: 750,
modal: true,
buttons : {
"Search" : function() {
$.ajax({
url : '<s:url action="part" />',
success : function(data) {
//var obj = $.parseJSON(data);
var obj = data;
alert(JSON.stringify(obj));
}
});
}
}
});
});
</script>
Returning json
result type is not needed if you build the JSONObject
manually. You can return text as stream result then convert a string to JSON if needed.
struts.xml
:
<package name="default" extends="struts-default">
<action name="part" class="action.PartAction" method="finder">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">stream</param>
</result>
</action>
</package>
Action:
public class PartAction extends ActionSupport {
public class SearchResult {
private String col1;
private String col2;
public String getCol1() {
return col1;
}
public void setCol1(String col1) {
this.col1 = col1;
}
public String getCol2() {
return col2;
}
public void setCol2(String col2) {
this.col2 = col2;
}
public SearchResult(String col1, String col2) {
this.col1 = col1;
this.col2 = col2;
}
}
private InputStream stream;
//getter here
public InputStream getStream() {
return stream;
}
private List<SearchResult> findList = new ArrayList<>();
public List<SearchResult> getFindList() {
return findList;
}
public void setFindList(List<SearchResult> findList) {
this.findList = findList;
}
private String list() {
JSONObject jo = new JSONObject();
try {
for (SearchResult part : findList) {
jo.put("col1", part.getCol1());
jo.put("col2", part.getCol2());
}
System.out.println("--------->:"+jo.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return jo.toString();
}
@Action(value="part", results = {
@Result(name="stream", type="stream", params = {"contentType", "text/html", "inputName", "stream"}),
@Result(name="stream2", type="stream", params = {"contentType", "application/json", "inputName", "stream"}),
@Result(name="json", type="json", params={"root", "findList"})
})
public String finder() {
findList.add(new SearchResult("val1", "val2"));
stream = new ByteArrayInputStream(list().getBytes());
return "stream2";
}
}
I have placed different results with result type and content type to better describe the idea. You could return any of these results and return JSON object either stringified or not. The stringified version requires to parse returned data to get the JSON object. You can also choose which result type better serializes to suit your needs but my goal was to show that if you need to serialize the simple object then json plugin is not necessary to get it working.
References:
- How can we return a text string as the response
- How to convert
JSONObject
to string
What could be causing a Not result defined for action and result error response?
The error means that the action com.personal.app.action.MyAction
returned the response of ERROR
as the dispatcher code and Struts2 is telling you that you don't have an action mapping defined for that named result.
You either need to:
- Define a global result named error.
- Define a result mapping for your action.
For the latter, it would look something like:
<action name="whatever" class="com.personal.app.action.MyAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
Hope that helps.
struts2-rest-plugin failing: org.apache.struts2.dispatcher.Dispatcher - Could not find action or result
OK - the entire problem was that I adopted sample code in a way that "seemed reasonable".
But it turns out that the struts2-rest-plugin has many "unspoken conventions" which I wasn't aware of. Specifically:
By default, struts2-rest-plugin maps pre-ordained action names
index()
,show()
,create()
,update()
anddestroy()
to CRUD operations.I did NOT need to define a "package" or any "actions" in struts.xml. Unless I need to customize, Struts2-rest-plugin wants to manage these details itself. This is the (minimal!) struts.xml I wound up with:
I did NOT want to use .jsp's (like the examples). I thought needed to "return something" (like a JSON response) from each action.
Instead, I just needed to redirect to an action. For example:
public HttpHeaders create() {
log.debug("Creating new contact...", model);
contactsRepository.addContact(model);
return new DefaultHttpHeaders("index");
...Most important, I wasn't clear about the struts2-rest-plugin URI conventions to invoke each different CRUD operation:
Struts-rest-plugin URL mappings (https://struts.apache.org/plugins/rest/):
**Default**
**Method** **Description** **Example**
------ ----------- -------
index() GET request with no id parameter. GET http://localhost:8080/StrutsContactsApp/contacts.json
show() GET request with an id parameter. GET http://localhost:8080/StrutsContactsApp/contacts/1.json
create() POST request with no id parameter and JSON/XML body. POST http://localhost:8080/StrutsContactsApp/contacts.json
update() PUT request with an id parameter and JSON/XML body. PUT http://localhost:8080/StrutsContactsApp/contacts/65.json
destroy() DELETE request with an id parameter. DELETE http://localhost:8080/StrutsContactsApp/contacts/33.json
edit() GET request with an id parameter and the edit view specified.
editNew() GET request with no id parameter and the new view specified.
Struts-rest-plugin runtime automatically manages:
- Parsing object ID from REST URI
- Serializing/deserializing input parameters and return objects
- By default, controller will implement interface com.opensymphony.xwork2.ModelDriven
- By default, shouldn't need to (i.e. *shouldn't*) declare any packages or actions in struts.xml
<= "Follow conventions", and struts2-rest-plugin will manage all the details...
No result defined for action and result input for validation
I believe you implemented validate()
in your action class example.Registration
right?
Even though you specified to call the method validateEmailValue()
on your action validateEmailValue
, it will first run validate()
and check if the validation succeed, and will forward the page to INPUT
result if validation failed.
Since your validateEmailValue
action do not have a INPUT
result declared, system throw the error you saw.
Try adding the INPUT
result to your validateEmailValue
action and see what is shown.
<result name="input" type="json"/>
Return a string from Struts2 action to jQuery
You can use stream
result to get just a String from the action.
Configure your action to use stream
result with contentType
set to text/plain
(or don't use contentType
at all, because text/plain
is set by default).
<action name="callAction" method="call">
<result type="stream">
<param name="contentType">text/plain</param>
</result>
</action>
In your action create InputStream
field with getter/setter and in your action method convert String to the input stream.
private InputStream inputStream;
// getter/setter
public String callAction() {
inputStream = new ByteArrayInputStream(
"some string".getBytes(StandardCharsets.UTF_8));
return SUCCESS;
}
Then you can execute ajax request like that:
$.ajax ({
url: '<s:url action="callAction"/>',
type: 'POST',
dataType: 'text',
success: function (data) {
console.log(data);
}
});
Note: it is better to use <s:url>
tag to construct url-s and there isn't such dataType
as string
, use text
or don't set it at all (jQuery will try to infer it based on the MIME type of the response).
An item with the same key has already been added
Most likely, you have model which contains the same property twice. Perhaps you are using new
to hide the base property.
Solution is to override the property or use another name.
If you share your model, we would be able to elaborate more.
Renaming column names in Pandas
Just assign it to the .columns
attribute:
>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
>>> df
$a $b
0 1 10
1 2 20
>>> df.columns = ['a', 'b']
>>> df
a b
0 1 10
1 2 20
Unable to send back array of in struts2 json plugin
First of all don't name your object fields with single lower case letter (e.g. sArray
). Change names to something different (e.g. strArray
). In this way you don't need to guess what getters/setters names should be.
Second, the correct syntax for the includeProperties
attribute of struts2-json-plugin
for arrays is ^array\[\d+\]
.
So if your fields are strArray
and intArray
the configuration should be something like that:
<package name="name" namespace="/" extends="json-default">
<action name="Test"
class="com.actions.TestAction">
<result name="success" type="json">
<param name="excludeNullProperties">true</param>
<param name="includeProperties">^strArray\[\d+\],^intArray\[\d+\],value</param>
</result>
</action>
</package>
Related Topics
Deserialize a List≪T≫ Object With Gson
How to Retrieve a File from a Server Via Sftp
When to Use Stringbuilder in Java
Find Where Java Class Is Loaded From
How to Avoid Installing "Unlimited Strength" Jce Policy Files When Deploying an Application
Java: How to Get a Class Literal from a Generic Type
Java: How to Get Input from System.Console()
How to Access Static Resources When Mapping a Global Front Controller Servlet on /*
Platform.Runlater and Task in Javafx
Can You Use @Autowired With Static Fields
How to Use Swingworker in Java
Java.Lang.Outofmemoryerror: Java Heap Space