Struts2 json ajax 以及自定义返回类型
我正在尝试从 struts2 操作中执行“json”结果类型。这是我的代码中的配置。
Struts.xml:
<package name="example" namespace="/" extends="json-default">
<action name="vendorList" class="com.stg.providerportal.actions.AjaxVendorListAction">
<result name="success" type="json"></result>
</action>
</package>
Struts 操作:
private Object jsonModel;
public String execute() throws Exception {
String nextStep = SUCCESS;
Map session = getSession();
SessionParams params = (SessionParams) session.get(Constants.KEY_SESSION_PARAMS);
try {
Map json = new HashMap<String,String>();
json.put("apple", "Hi");
//vendorList = populateVendors(params,taxID,billingZipCode);
setJsonModel(json);
//session.put(Constants.KEY_SESSION_VENDOR_LIST, vendorList);
} catch (Exception e) {
CILogUtil.logInfo(e, params);
nextStep = AppConstants.SYSTEM_UNAVAILABLE;
}
return SUCCESS;
}
我使用 ajax(jquery) 调用此操作,这里是我如何进行调用。
$.getJSON('vendorList.action' ,
{
"taxID": taxID,
"billingZipCode": billingZipCode
},
function(json) {
alert('hi');
});
$.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {
alert(textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
}});
当我执行此操作时,我会收到以下操作的响应。不知道为什么。
Error 404: SRVE0190E: File not found: /vendorList.action
即使当我尝试直接从浏览器运行此操作时,我也会得到相同的响应。
http://localhost:9081/providerportal1/vendorList.action?taxID=111billingZipCode=1111
I am trying to do a result type of "json" from my struts2 action. Here is the configuration I have in my code.
Struts.xml:
<package name="example" namespace="/" extends="json-default">
<action name="vendorList" class="com.stg.providerportal.actions.AjaxVendorListAction">
<result name="success" type="json"></result>
</action>
</package>
Struts Action:
private Object jsonModel;
public String execute() throws Exception {
String nextStep = SUCCESS;
Map session = getSession();
SessionParams params = (SessionParams) session.get(Constants.KEY_SESSION_PARAMS);
try {
Map json = new HashMap<String,String>();
json.put("apple", "Hi");
//vendorList = populateVendors(params,taxID,billingZipCode);
setJsonModel(json);
//session.put(Constants.KEY_SESSION_VENDOR_LIST, vendorList);
} catch (Exception e) {
CILogUtil.logInfo(e, params);
nextStep = AppConstants.SYSTEM_UNAVAILABLE;
}
return SUCCESS;
}
I am calling this action using ajax(jquery) and here how i make a call.
$.getJSON('vendorList.action' ,
{
"taxID": taxID,
"billingZipCode": billingZipCode
},
function(json) {
alert('hi');
});
$.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {
alert(textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
}});
When i execute this I am getting back the response from the action as below. Dont know why.
Error 404: SRVE0190E: File not found: /vendorList.action
Even when I am trying to run this action directly from the browser, I get the same response.
http://localhost:9081/providerportal1/vendorList.action?taxID=111billingZipCode=1111
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在 web.xml 文件中正确配置了 struts2 过滤器?至少,您应该像此示例一样配置 web.xml -
http://s.apache.org/ ata
另外,当网络应用程序启动时,您在 tomcat 日志中是否看到任何错误?通常,如果 struts2 过滤器找不到与请求匹配的操作定义,它将创建如下错误 -
HTTP 状态 404 - 没有为命名空间 / 和操作名称vendorList.action 映射的操作。
您粘贴的 404 看起来更像是来自 Tomcat,这表明 struts2 可能没有正确合并。
Do you have the struts2 filter(s) configured appropriately in your web.xml file? At a minimum, you should have your web.xml configured like this example -
http://s.apache.org/ata
Also, did you see any errors in your tomcat logs when the web-app started? Typically, if the struts2 filter can't find an action definition to match the request, it will create an error like the following -
HTTP Status 404 - There is no Action mapped for namespace / and action name vendorList.action.
The 404 you pasted looks more like it came from Tomcat which would indicate that struts2 is probably not properly incorporated.