我的添加函数不适用于 sjg-struts2



你好,这是我的行动课

package com.pfs.action;
import com.opensymphony.xwork2.ActionSupport;
import com.pfs.dao.userDAO;
import com.pfs.model.User;
public class CrudAction extends ActionSupport {
    private int id;
    private String username;
    private String password;
    private String fullname;
    private String address;
    private String city;
    private String state;
    private String oper;
    userDAO dao = new userDAO();

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getFullname() {
        return fullname;
    }
    public void setFullname(String fullname) {
        this.fullname = fullname;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getOper() {
        return oper;
    }
    public void setOper(String oper) {
        this.oper = oper;
    }
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        User u;
        if (oper.equalsIgnoreCase("add")) {
            System.out.println("Add Function");
            u = new User();
            u.setUsername(username);
            u.setPassword(password);
            u.setFullname(fullname);
            u.setAddress(address);
            u.setCity(city);
            u.setState(state);
            dao.addUser(u);
        } else if(oper.equalsIgnoreCase("edit")) {
            System.out.println("Edit Function");
            u = dao.findByID(id);
            u.setUsername(username);
            u.setPassword(password);
            u.setFullname(fullname);
            u.setAddress(address);
            u.setCity(city);
            u.setState(state);
            dao.editUser(u);
        } else if(oper.equalsIgnoreCase("del")) {
            System.out.println("Delete Function");
            dao.delUser(id);
        }
        return NONE;    
    }
}

这是我的JSP页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<sj:head jqueryui="true" jquerytheme="start" />
<title>User Management</title>
</head>
<body>
    <s:url var="remoteurl" action="jsontable" />
    <s:url var="editUrl" action="crud" />
    <sjg:grid id="gridtable" caption="User List" dataType="json"
        href="%{remoteurl}" pager="true" gridModel="userList"
        rowList="10,15,20" rowNum="15" rownumbers="true" resizable="true"
        draggable="true" droppable="true" navigator="true"
        navigatorView="true" navigatorAdd="true" navigatorEdit="true"
        navigatorDelete="true" navigatorViewOptions="{height:280, width:500}"
        editurl="%{editUrl}">
        <sjg:gridColumn name="id" index="id" title="UserID" sortable="true"
            align="center" key="true" formatter="integer" hidden="true"/>
        <sjg:gridColumn name="username" index="username" title="Username"
            sortable="false" align="center" editable="true" />
        <sjg:gridColumn name="password" index="password" title="Password"
            sortable="false" align="center" editable="true" edittype="password" />
        <sjg:gridColumn name="fullname" index="fullname" title="Fullname"
            sortable="false" align="center" editable="true" />
        <sjg:gridColumn name="address" index="address" title="Address"
            sortable="false" align="center" editable="true" />
        <sjg:gridColumn name="city" index="city" title="City" sortable="false"
            align="center" editable="true" />
        <sjg:gridColumn name="state" index="state" title="State"
            sortable="false" align="center" editable="true" edittype="select" 
            editoptions="{value:'France:France;USA:USA;Australia:Australia;Norway:Norway;Poland:Poland;Germany:Germany;Spain:Spain;Vietnam:Vietnam'}" />
    </sjg:grid>
</body>
</html>

最后是支柱.xml

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <package name="showcase" extends="struts-default,json-default"
        namespace="/">
        <action name="jsontable" class="com.pfs.action.JsonTable">
            <result name="success" type="json">/ShowList.jsp</result>
        </action>
        <action name="crud" class="com.pfs.action.CrudAction">
            <result name="none" >/ShowList.jsp</result>
            <result name="input" >/ShowList.jsp</result>
        </action>
    </package>
</struts>

在此之前,我可以执行添加函数,但无法执行编辑或删除函数。现在我修复了可以执行的编辑和删除功能,但我的添加功能没有。有人可以帮助我T_T对不起我的英语不好-_-

了解您更改了哪些内容以使编辑/del工作但导致添加失败会很有帮助。

我会从三件事开始:首先,属性private int id;应该使用Integer而不是int。添加时,该值可能null,并且不能将基元类型设置为 null。其次,您的execute()方法应返回SUCCESS而不是NONE。第三,与其让 execute() 方法抛出异常,不如使用 try/catch 处理任何潜在的异常。

最新更新