Struts 2:无法在jsp页面中显示表格

时间:2016-09-09 15:01:15

标签: hibernate jsp struts2 crud

我指的是与http://www.tutorials4u.net/struts2-tutorial/struts2_crud_example.html提到的相同的例子。 问题出在我的jsp中,我无法在数据库中插入记录后在jsp页面中显示表,我无法遍历列表 image of table missing from jsp page

registertodo.jsp是: -

<s:bean name="com.model.TODO" var="todo" />
<s:form action="saveorupdateTodo">
    <s:push value="todo">
        <s:hidden name="ID" />
        <s:textfield name="TITLE" label="title" />
        <s:radio name="COMPLETE" label="completed" list="{'1','0'}" />
        <s:submit />
    </s:push>
</s:form>



    <div class="content">
        <table class="todoTable" cellpadding="5px">


            <tr class="even">
                <th>TITLE</th>
                <th>STATUS</th>
                <th>EDIT</th>
                <th>DELETE</th>
            </tr>


            <s:iterator value="todolist">

                <tr>

                    <td><s:property value="TITLE" /></td>

                    <td><s:property value="COMPLETE" /></td>
                    <td><s:url id="editURL" action="editTodo">
                            <s:param name="id" value="%{ID}"></s:param>
                        </s:url> <s:a href="%{editURL}">Edit</s:a></td>
                    <td><s:url id="deleteURL" action="deleteTodo">
                            <s:param name="id" value="%{ID}"></s:param>
                        </s:url> <s:a href="%{deleteURL}">Delete</s:a></td>

                </tr>
            </s:iterator>
            </tbody>
        </table>
    </div>

我的struts.xml是: -

<struts>
<package name="default" extends="hibernate-default">
    <action name="saveorupdateTodo" class="com.action.JtableAction"
        method="create">
        <result name="success" type="redirect">listTodo</result>
    </action>
    <action name="listTodo" class="com.action.JtableAction" method="list">
        <result name="success" type="redirect">/registertodo.jsp</result>
    </action>
    <action name="updateTodo" class="com.action.JtableAction"
        method="update">
        <result name="success" type="redirect">/registertodo.jsp</result>
    </action>
    <action name="deleteTodo" class="com.action.JtableAction"
        method="delete">
        <result name="success" type="redirect">listTodo</result>
    </action>

</package>
</struts>

我的JtableAction类是: -

public class JtableAction extends ActionSupport implements ModelDriven<TODO> {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private TODO todo = new TODO();
private List<TODO> todolist = new ArrayList<TODO>();
private CrudDao dao = new CrudDao();

public TODO getModel() {

    return todo;
}

public String list() {
    try {
        // Fetch Data from Student Table
        todolist = dao.getAllTODOs();
        System.out.println(todolist);
    } catch (Exception e) {
        System.err.println(e.getMessage());

    }
    return Action.SUCCESS;
}

// Creating any new Record
public String create() throws IOException {

    try {
        // Create new record
        todo.setID();
        dao.addTODO(todo);
    } catch (Exception e) {

        System.err.println(e.getMessage());
    }
    return Action.SUCCESS;
}

// Updating Record
public String update() throws IOException {

    try {
        // Update existing record
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext()
                .get(ServletActionContext.HTTP_REQUEST);
        todo = dao.listUserById(request.getParameter("ID"));
    } catch (Exception e) {

        System.err.println(e.getMessage());
    }
    return Action.SUCCESS;
}

// deleting Record

public String delete() throws IOException {
    // Delete record
    try {
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext()
                .get(ServletActionContext.HTTP_REQUEST);
        dao.deleteTODO(request.getParameter("ID"));
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    return Action.SUCCESS;
}

// getters and setters
public TODO getTodo() {
    return todo;
}

public void setTodo(TODO todo) {
    this.todo = todo;
}

public List<TODO> getTodolist() {
    return todolist;
}

public void setTodolist(List<TODO> todolist) {
    this.todolist = todolist;
}

1 个答案:

答案 0 :(得分:0)

在index.jsp页面中,我添加了

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags"%>

在我的struts.xml中删除了type =“redirect”,它重定向到了registertodo.jsp

    <action name="listTodo" class="com.action.JtableAction" method="list">
        <result name="success">/registertodo.jsp</result>
    </action>