2014-03-31 35 views
0

嗨, 這裏是我的動作類我的附加功能不要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> 

和決賽struts.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對不起我的英語不好-_-

回答

0

這將有助於知道您更改了哪些內容以進行編輯/刪除工作,但導致添加失敗。

我會從三件事開始:首先,屬性private int id;應該使用Integer而不是int。添加時,值可能爲null,並且您不能將基元類型設置爲空。其次,您的​​方法應返回SUCCESS而不是NONE。第三,不要讓​​方法拋出異常,你應該用try/catch處理任何潛在的異常。