精通Structs中addressbook:删除功能的怎么实现?
前提xml中各组件已连上了,个常量也对应好了下面的代码可是不能删除啊???
1.
package addressbook.forms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public final class DelForm extends ActionForm {
private String name = null;
private String phone = null;
private String address = null;
private String hunk = null;
private String delId = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getHunk() {
return address;
}
public void setHunk(String hunk) {
this.hunk = hunk;
}
public String getDelId() {
return delId;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
//name = null;
//phone = null;
//address = null;
}
public void setDelId(String delId) {
this.delId = delId;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (((delId == null)))
errors.add( "name ", new ActionMessage( "error.name.required "));
// if(((phone == null)|| (phone.length() < 1)))
// errors.add( "phone ", new ActionMessage( "error.phone.required "));
//if(((address == null)|| (address.length() < 1)))
// errors.add( "address ", new ActionMessage( "error.address.required "));
return errors;
}
2.
2.
import addressbook.Constants;
包都蹈进了....
public final class DelAction extends AbstActionBase {
private Log log =
LogFactory.getLog(this.getClass().getName());
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
ActionMessages errors = new ActionMessages();
String name =((DelForm) form).getName();
String delId = ((DelForm) form).getDelId();//
//String deleteId = "8 ";
String phone = ((DelForm) form).getPhone();
String address=((DelForm)form).getAddress();
String hunk = ((DelForm) form).getHunk();
if (!errors.isEmpty()) {
//saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
String strSql = new String( "delete FROM " + Constants.TABLENAME + " WHERE ");
//error:IOException:java.sql.SQLException: Can not issue data manipulation statements with executeQuery()
//if (!name.equals( " "))
strSql = strSql + "name = "+ name + " ";//id + delId +
//if (!phone.equals( " "))
//strSql = strSql + " phone LIKE ' "+ phone + "% ' AND ";
//if (!address.equals( " "))
//strSql = strSql + " address LIKE ' "+ address + "% ' ";
//else
//strSql = strSql.substring(0,strSql.length()-3);
//strSql = strSql + "ORDER by ID "; //java.lang.NullPointerException
strSql = strSql.substring(0,strSql.length());//删除
//try
//{
AddressBookBean bean=new AddressBookBean(delId,phone,address,hunk,delId);
//bean.delete(strSql);
AddressBookBean.delete(strSql);//访问静态??
//}
//catch(Exception ex)
//{
// ex.printStackTrace(System.out);
// errors.add(ActionMessages.GLOBAL_MESSAGE,
// new ActionMessage( 'error.ioException '));
// }
HttpSession session = request.getSession(); //??????????
if (log.isDebugEnabled()) {
log.debug( "SearchAction session = " + session);
log.debug( "SearchAction strSql = " + strSql);
}
session.setAttribute(Constants.SQLSTMT_KEY, strSql);//SQLSTMT_KEY = "sqlstatement "????????
return (mapping.findForward(Constants.FORWARD_DELOK));//FORWARD_SUCCESS
}
}
}
[解决办法]
struts config 文件呢?