我正在使用struts2-jquery-grid-3.7.0插件尝试使用Struts jQuery 网格进行CRUD 操作,如showcase Grid (Editable/Multiselect) 上所示。
这是表格:
<s:form namespace="/admin_side" action="Test" validate="true" id="dataForm" name="dataForm">
<s:url id="remoteurl" action="TestGrid" namespace="/admin_side"/>
<s:url id="editurl" action="EditTest"/>
<sjg:grid
id="gridmultitable"
caption="Example (Editable/Multiselect)"
dataType="json"
href="%{remoteurl}"
pager="true"
navigator="true"
navigatorSearchOptions="{sopt:['eq','ne','lt','gt']}"
navigatorEdit="true"
navigatorView="true"
navigatorAddOptions="{height:280, width:500, reloadAfterSubmit:true}"
navigatorEditOptions="{height:280, width:500, reloadAfterSubmit:false}"
navigatorViewOptions="{height:280, width:500}"
navigatorDelete="true"
navigatorDeleteOptions="{height:280, width:500,reloadAfterSubmit:true}"
gridModel="gridModel"
rowList="5,10,15"
rowNum="5"
rownumbers="true"
editurl="%{editurl}"
editinline="true"
multiselect="true"
onSelectRowTopics="rowselect">
<sjg:gridColumn name="countryId" index="countryId" title="Id" formatter="integer" editable="false" dataType="Long" sortable="true" search="true" sorttype="integer" searchoptions="{sopt:['eq','ne','lt','gt']}"/>
<sjg:gridColumn name="countryName" index="countryName" title="Country Name" editable="true" sortable="true" search="true" sorttype="text"/>
<sjg:gridColumn name="countryCode" index="countryCode" title="Country Code" sortable="true" search="true" editable="true" sorttype="text"/>
</sjg:grid>
</s:form>
映射相应动作类中的一个方法,调用该方法,同时执行行的添加、删除和编辑等操作。
@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value = "json-package")
@InterceptorRefs(
@InterceptorRef(value = "store", params = {"operationMode", "AUTOMATIC"}))
public final class TestAction extends ActionSupport implements Serializable, ModelDriven<Country>
{
@Autowired
private final transient CountryService countryService=null;
private static final long serialVersionUID = 1L;
private Country entity=new Country();
private List<Country> gridModel=new ArrayList<Country>();
private String oper; //Getter and setter.
@Action(value = "EditTest",
results = {
@Result(name = ActionSupport.SUCCESS, location = "Test.jsp"),
@Result(name = ActionSupport.INPUT, location = "Test.jsp")},
interceptorRefs = {
@InterceptorRef(value = "defaultStack", params = {"validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})})
public String edit() throws Exception {
System.out.println(entity.getCountryId()+" : "+entity.getCountryName()+" : "+entity.getCountryCode()+" : "+oper);
if(oper.equalsIgnoreCase("add")) {
//Adding a row.
}
else if(oper.equalsIgnoreCase("edit")) {
//Editing/updating a row.
}
else if(oper.equalsIgnoreCase("del")) {
//Deleting a row.
}
return ActionSupport.SUCCESS;
}
@Override
public Country getModel() {
return entity;
}
@Action(value = "Test",
results = {
@Result(name = ActionSupport.SUCCESS, location = "Test.jsp"),
@Result(name = ActionSupport.INPUT, location = "Test.jsp")},
interceptorRefs = {
@InterceptorRef(value = "defaultStack", params = {"validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})})
public String load() throws Exception {
//This method is required to return an initial view on page load. Nothing to see here. Leave it empty.
return ActionSupport.SUCCESS;
}
}
删除时,ID(在本例中countryId
为 )始终为null
。
编辑时,countryId
为空,因为我设置editable="false"
为网格的相应列。当它设置为 时true
,它会被检索,但由于它是countryId
数据库中的主键,因此不应对其进行编辑。
删除和编辑时如何获取此ID(编辑时不应编辑)?
这不再特别相关ModelDriven
。
编辑:
在编辑和删除id
操作类中的字符串类型字段时,例如,
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
被初始化为相关网格的行 ID(选定行的)。
在删除多行时,将其初始化为由选定 ID 组成的逗号分隔字符串。
基于网格的行 ID 执行操作是不可靠的。它们应该基于数据库中的主键值来完成。这可能吗?