Structs中:删除功能的实现

2008-02-23 09:30:22来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

Structs中:删除功能的实现

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;

/**
* <strong>DeleteForm</strong> handles the form
* that the user will use to search the database.
*/

public final class DeleteForm extends ActionForm {

private String name = null;
private String phone = null;
private String address = 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 void reset(ActionMapping mapping, HttpServletRequest request) {

this.name = null;
this.phone = null;
this.address=null;

}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {

ActionErrors errors = new ActionErrors();
if (
((name == null) || (name.length() < 1))
&&((phone == null)|| (phone.length() < 1))
&&((address == null) || (address.length() < 1))
)
errors.add("search", new ActionMessage("error.searchcriteria.required"));
return errors;

}
}

Action 文件

import java.io.IOException;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.MissingResourceException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.MessageResources;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import addressbook.Constants;
import addressbook.forms.DeleteForm;
import addressbook.model.AddressBookBean;

/**
* <strong>DeleteAction</strong> will take the search parameters
* specified by the user and create the Sql statement to be used
* by the appropriate forward.
*/
public final class DeleteAction 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 = ((DeleteForm) form).getName();
String phone = ((DeleteForm) form).getPhone();
String address=((DeleteForm)form).getAddress();

if (!errors.isEmpty()) {
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}

String strSql = new String("DELETE FROM " Constants.TABLENAME " WHERE ");
/*
if (!name.equals(""))
strSql = strSql "name LIKE '" name "%' AND";
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";
*/
strSql = strSql "name='" name "'";
strSql = strSql.substring(0,strSql.length());

try
{
AddressBookBean bean=new AddressBookBean(name,phone,address);
bean.delete(strSql);
}
catch(Exception ex)
{
ex.printStackTrace(System.out);
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("error.delete.failed"));
}


HttpSession session = request.getSession();
if (log.isDebugEnabled()) {
log.debug("SearchAction session = " session);
log.debug("SearchAction strSql = " strSql);

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:java.util.Collection翻译

下一篇:Websphere Eclipse远程调试