以上只是简单介绍了各个层的划分以及各层所对应的开源框架,要详细了解各层的具层信息,需要访问 struts,hibernate,spring的官方网站。
CREATE DATABASE `test`
USE `test`;
`id` int(11) NOT NULL auto_increment,
`username` varchar(30) NOT NULL default '',
`password` varchar(30) default '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
package service; import java.util.List; import vo.User; import dao.UserDAO; public class Service { private UserDAO userDao; public UserDAO getUserDao() { return userDao; } public void setUserDao(UserDAO userDao) { this.userDao = userDao; } public boolean isValid(User user) {//判断用户是否合法 List result = userDao.findByExample(user); if (result.size() > 0) return true; else return false; } } |
<bean id="service" class="service.Service" singleton="false">
<property name="userDao">
<ref bean="userDao" />
</property>
</bean>
<action attribute="loginForm" input="/login.jsp" name="loginForm" path="/login" scope="request" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="fail" path="/login.jsp" /> <forward name="succeed" path="/welcome.jsp" /> </action> |
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" /> </plug-in>> 6.2修改applicationContext,增加以下内容 <bean name="/login" class="web.action.LoginAction" singleton="false"> <property name="service"> <ref bean="service" /> </property> </bean> |
/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package web.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import web.form.LoginForm; import service.Service; import vo.User; /** * MyEclipse Struts Creation date: 09-27-2006 * * XDoclet definition: * * @struts.action path="/login" name="loginForm" input="/login.jsp" * scope="request" validate="true" * @struts.action-forward name="fail" path="/login.jsp" * @struts.action-forward name="succeed" path="/welcome.jsp" */ public class LoginAction extends Action { /* * Generated Methods */ /** * Method execute * * @param mapping * @param form * @param request * @param response * @return ActionForward */ private Service service; public void setService(Service service) { this.service = service; } public Service getService() { return service; } public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method // stub User user = new User(); user.setPassword(loginForm.getPassword()); user.setUsername(loginForm.getUserName()); if (service.isValid(user)) return mapping.findForward("succeed"); else return mapping.findForward("fail"); } } |