1.表示层(应用了template)
JSP文件(template/Basictemplate.jsp)
<%@ page contentType="text/html; charset=gb2312" %> <%@ taglib uri='/WEB-INF/struts-template.tld' prefix='template' %> <html> <head> <title><template:get name='title'/></title> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=gb2312"> <META HTTP-EQUIV="Content-Language" CONTENT="zh-CN"> <META name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <META name="keywords" content="extreme programming,
extreme projects, design pattern, J2EE, forum, chinese"> <LINK href="default.css" type="text/css" rel="stylesheet"> </head> <body topmargin='0'> <table width="95%" border="0" align="center"> <tr> <td width="17%" rowspan="3"> <!-- START OF NAVBAR --> <template:get name='navbar'/> <!-- END OF NAVBAR --> </td> <td width="83%" valign="top"> <!-- START OF HEADER --> <template:get name='header'/> <!-- END OF HEADER --> </td> </tr> <tr> <td valign="top"> <!-- START OF CONTENT --> <template:get name='content'/> <!--END OF CONTENT --> </td> </tr> <tr> <td valign="top"> <!-- START OF FOOTER --> <template:get name='footer'/> <!-- END OF FOOTER --> </td> </tr> </table> </body></html>
JSP文件(addDemo.jsp)
<% /** * <p>Title: Struts开发测试1.0</p> * <p>Description:项目说明 </p> * <p>Copyright: Copyright (c) 2003-2008 </p> * <p>Company:优势科技 </p> * @author 段洪杰 * @version 1.0 * @time [2003年10月22日 14时50分] **/ %> <%@page contentType="text/html; charset=gb2312" %> <%@taglib uri='/WEB-INF/struts-template.tld' prefix='template' %> <template:insert template='/template/basicTemplate.jsp'> <template:put name='title' content='Struts开发测试1.0' direct='true'/> <template:put name='navbar' content='/content/navbar.jsp' /> <template:put name='header' content='/content/header.jsp' /> <template:put name='content' content='/content/addDemo.jsp'/> <template:put name='footer' content='/content/footer.jsp' /> </template:insert>
JSP文件(content/addDemo.jsp)
<% /** * <p>Title: Struts开发测试1.0</p> * <p>Description:项目说明 </p> * <p>Copyright: Copyright (c) 2003-2008 </p> * <p>Company:优势科技 </p> * @author 段洪杰 * @version 1.0 * @time [2003年10月22日 14时50分] **/ %> <%@page contentType = "text/html; charset=GB2312" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@taglib uri="/WEB-INF/ImageTag.tld" prefix="image" %> <%@taglib uri="/WEB-INF/DateTag.tld" prefix="date" %> <jsp:useBean id="sessionBean" scope="session"
class="com.company.demo.web.SessionBean" /> <html:errors/> <FORM METHOD="post" ACTION="addDemoAction.do" > <p>名字(name) : <input name="name" type="text"> </p> <p>日期(date) : <date:inputButton type="date" name="userDate"/> </p> <p>日期(date) : <date:inputButton type="datetime" name="userDateTime"/> </p> <p> <image:uploadButton savePath="saveimagepath"/> </p> <p> <input type="submit" name="Submit" value="提交"> <input type="reset" name="Submit2" value="重置"> </p> </FORM>
控制层(struts-config.xml)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//
DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/
struts/dtds/struts-config_1_0.dtd"> <struts-config> <form-beans> <form-bean name="demoActionForm" type="com.company.demo.web.DemoActionForm" /> <form-bean name="imageUploadActionForm"
type="com.company.demo.web.ImageUploadActionForm" /> </form-beans> <global-forwards> <forward name="unknown-error" path="/error.jsp" /> <forward name="success" path="/success.jsp" /> </global-forwards> <action-mappings> <action name="demoActionForm" type="com.company.demo.web.AddDemoAction"
input="/addDemo.jsp" scope="request" path="/addDemoAction" /> <action name="imageUploadActionForm" type="com.company.demo.web.ImageUploadAction"
input="/content/imageUpload.jsp" scope="request" path="/imageUploadAction" /> </action-mappings> </struts-config>
文件(DemoActionForm.java)
package com.company.demo.web; import org.apache.struts.action.*; import javax.servlet.http.*; public class DemoActionForm extends ActionForm { private String name; private String userDate; private String userDateTime; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUserDate() { return userDate; } public void setUserDate(String userDate) { this.userDate = userDate; } public String getUserDateTime() { return userDateTime; } public void setUserDateTime(String userDateTime) { this.userDateTime = userDateTime; } public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) { /**@todo: finish this method, this is just the skeleton.*/ return null; } public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest)
{ } }
应用逻辑层(AddDemoAction.java)
package com.company.demo.web; import org.apache.struts.action.*; import javax.servlet.http.*; import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import com.company.demo.jdo.Demo; import com.company.demo.dao.DemoDAO; import com.company.demo.dao.DemoDAOFactory; import com.company.demo.dao.DemoDAOImpl; public class AddDemoAction extends Action { public ActionForward perform(ActionMapping actionMapping, ActionForm actionForm,
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { /**@todo: complete the business logic here, this is just a skeleton.*/ DemoActionForm demoActionForm = (DemoActionForm) actionForm; SessionBean sessionBean = (SessionBean) httpServletRequest.getSession().getAttribute("
sessionBean"); if (sessionBean == null) { httpServletRequest.setAttribute("message", "系统超时,请重新登录!!!"); return actionMapping.findForward("unknown-error"); } else { try { Demo demo = new Demo(); demo.setName(demoActionForm.getName()); DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd hh:ss:mm"); Date userDateTime = dateTimeFormat.parse(demoActionForm.getUserDateTime()); demo.setSystemDate(userDateTime); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date userDate = dateFormat.parse(demoActionForm.getUserDate()); demo.setUserDate(userDate); DemoDAO demoDAO = DemoDAOFactory.getDemoDAO(); demoDAO.setDataModule(sessionBean.getDataModule()); demoDAO.createDemo(demo); } catch (Exception ex) { httpServletRequest.setAttribute("message",ex.toString()); return (actionMapping.findForward("unknown-error")); } httpServletRequest.setAttribute("message", "成功!"); return (actionMapping.findForward("success")); } } }