我嘗試使用Struts jQuery網格使用struts2-jquery-grid-3.7.0插件進行CRUD操作,如showcase Grid(可編輯/多選)所示。使用Struts2添加,編輯和刪除網格行jQuery Grid插件
這是以下形式:
<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
不再特別相關。
編輯:
雖然編輯和刪除,在操作類似String類型id
字段,
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
被初始化爲行ID(所選擇的行的)有問題的電網。
在刪除多行時,它會被初始化爲由選定ID組成的以逗號分隔的字符串。
基於網格的行ID執行操作是不可靠的。他們應該根據數據庫中的主鍵值完成。這可能嗎?
雖然這是完全正確的,但是在刪除多行時,我們不能擁有一個'List'嗎?相反,它將動作類中的id字段初始化爲逗號分隔的字符串,同時刪除多行。例如,如果我將3行標記爲id爲1,2和3的刪除行,則動作類中的String類型的「id」字段將被初始化爲像「1,2,3」這樣的字符串。如果id字段的類型是「Long」或「List 」,那麼它將把這些id作爲整數如「123」。如果id的類型是'List ',那麼它將再次被初始化爲逗號分隔的字符串,比如'1,2,3'。 –
Tiny