电子通讯薄(MVC) :此示例基于MVC模式,包含四个界面:登入、添加数据、查询数据和显示所有数据,通过这个示例将会对Struts构架有进一步的了解
源代码在线查看: searchaction.java
package addressbook.actions; import addressbook.Constants; import addressbook.forms.*; import addressbook.model.*; import org.apache.struts.action.*; import javax.servlet.http.*; public class SearchAction extends Action { public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { /**@todo: complete the business logic here, this is just a skeleton.*/ SearchForm searchForm = (SearchForm) actionForm; String name=searchForm.getName(); String phone=searchForm.getPhone(); String address=searchForm.getAddress(); String strSql=new String("select * from book 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"; HttpSession session=httpServletRequest.getSession(); session.setAttribute("session",strSql); //System.out.println(session.getAttribute("session")); return (actionMapping.findForward("view")); } }