我的代码做得很好。但我对一个问题感到震惊。我想在 struts2 的选择框中填充 json 数据。
我正在做的是..我在单击按钮并显示表单时发送 ajax 请求。
表单最初具有属性 display none。当我单击按钮时,它会变为阻止。
但是每当我在浏览器中打开我的 jsp 页面时都会出现一个错误。
请求的列表键“类别”无法解析为集合
我的 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 文件
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;
}
}
我在jsp页面上的脚本
$(".add_sub_category").click(function(e){
e.preventDefault();
$.ajax({
url : '/Login/admin/categories',
type : 'POST',
dataType : 'json',
sucess : function(resp) {
console.log(resp);
}
});
});
请帮忙。我在哪里做错了?以及为什么我有这个问题。