I am working using Struts2-jquery-grid and I am able to edit / delete the existing entry. But I am facing problem to add new entry. I tried to figure out why the add functionality is not working. All of my codes are like the following:
gridTable.jsp
<s:url var="remoteurl" action="jsonFormatDate" />
<sj:head jqueryui="true" jquerytheme="redmond" />
<sjg:grid id="gridtable"
dataType="json"
href="%{remoteurl}"
pager="true"
gridModel="gridModel"
editurl="gridTableEdit"
navigator="true"
navigatorAdd="true"
navigatorEdit="true"
navigatorDelete="true"
>
<sjg:gridColumn name="bookID" index="bookID" title="bookID" sortable="true" editable="true" hidden="false" key="true"/>
<sjg:gridColumn name="bookTitle" index="bookTitle" title="bookTitle" editable="true" />
</sjg:grid>
Action layer [ridTable.java]:
public class GridTable extends ActionSupport {
int id;
String oper;
String bookTitle;
//getter and setter
// display the grid into the jsp page
public String gridTableDisplay() {
return SUCCESS;
}
public String gridTableEdit() {
if (oper.equalsIgnoreCase("add")) {
BookBusiness business = new Business();
business.addBook(bookTitle);
}
else if (oper.equalsIgnoreCase("edit")) {
BookBusiness business = new Business();
business.editBook(bookTitle);
}
else if (oper.equalsIgnoreCase("del")) {
BookBusiness business = new Business();
business.deleteBook(bookTitle);
}
return SUCCESS;
}
}
Business layer [BookBusiness.java]
public void addBook(String bookTitle) {
BookDAO dao = new BookDAO();
Book newBook = new Book();
newBook.setTitle(bookTitle)
dao.updateBook(newBook);
}
public Book editBook(....) {
...
}
public void deleteBook(...) {
....
}
DAO layer [BookDAO.java]
.....
I tried to execute the add functionality but failed. Can you tell me what exactly causing the problem? I executed the code from the business class and I find that the code is working finely and it is able to add new entry. But it fails to add form grid.
Even though you named the first column
bookId, the grid will useidfor the column that haskey=true. When you use the grid to perform an add,setId()will be called with a value of null. You cannot set a null to a primitive type, so you need to change yourint id;property and associated getter/setter to useIntegerinstead ofint. This should resolve your issue.