Tyring 一个简单的 json 示例,但它显示错误。我在类路径中包含了 gson-2.2.3.jar。
我正在使用 netbeans 7.1。该程序未部署。
Apache tomcat 日志显示:
Unable to load configuration. - action - file:/D:/APP/webApp1/build/web/WEB-INF/classes/struts.xml:10:73
Caused by: Error building results for action sayHi in namespace - action - file:/D:/APP/webApp1/build/web/WEB-INF/classes/struts.xml:10:73
Caused by: There is no result type defined for type 'json' mapped with name 'success'. Did you mean 'json'? - result - file:/D:/APP/webApp1/build/web/WEB-INF/classes/struts.xml:11:33
我正在尝试简单的例子。请看看是什么问题。动作类
public class AjaxActions extends ActionSupport {
private String name;
private String greeting;
public String sayHi() {
greeting = "HI " + name;
return ActionSupport.SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGreeting() {
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
}
Struts.xml
<package extends="struts-default,json-default" name="ajax-package" namespace="/ajax">
<action class="example.AjaxActions" method="sayHi" name="sayHi">
<result type="json">
</result>
</action>
</package>
在jsp文件中
<form action="" id="introForm">
<label for="name">Enter Your Name</label>
<input name="name">
<input type="submit">
</form>
<script>
$(function(){
$("#introForm").submit(function(){
var formInput=$(this).serialize();
$.getJSON('ajax/sayHi.action', formInput,function(data) {
$('.result').html('' + data.greeting + '');
return false;
});
});
</script>