




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、武漢紡織大學高級網(wǎng)站設計實驗報告班級: 姓名: 實驗時間:2014年5月7日 指導教師:陳岡一、實驗目的1.了解數(shù)據(jù)庫連接池。2. 掌握Spring應用。3. 掌握Hibernate應用。二、實驗內(nèi)容1 創(chuàng)建一個基于Spring MVC的Web應用項目。2 實現(xiàn)項目的國際化處理。3 實現(xiàn)用戶的登錄、注冊處理。4 編寫一個小型留言板系統(tǒng),利用Hibernate實現(xiàn)留言板處理。三、操作步驟.edu.wtu.cg.dao: Teacher類:package .wtu.cg.domain;public class Teacher private String jobTitle;privat
2、e String eno;private String ename;private String password;public String getEno() return eno;public void setEno(String eno) this.eno = eno;public String getEname() return ename;public void setEname(String ename) this.ename = ename;public String getJobTitle() return jobTitle;public void setJobTitle(St
3、ring jobTitle) this.jobTitle = jobTitle;public String getPassword() return password;public void setPassword(String password) this.password = password; Student類: package .wtu.cg.domain;public class Student private String eno;private String email;private String ename;private String password;priv
4、ate String address;public String getEname() return ename;public void setEname(String ename) this.ename = ename;public String getEno() return eno;public void setEno(String eno) this.eno = eno;public String getEmail() return email;public void setEmail(String email) this.email = email;public String get
5、Password() return password;public void setPassword(String password) this.password = password;public String getAddress() return address;public void setAddress(String address) this.address = address;Loginer類: package .wtu.cg.domain;public class Loginer private String eno;private String password;
6、private String Category;public String getEno() return eno;public void setEno(String eno) this.eno = eno;public String getPassword() return password;public void setPassword(String password) this.password = password;public String getCategory() return Category;public void setCategory(String category) C
7、ategory = category;BaseEnity類: package .wtu.cg.domain;public class BaseEntity private Integer id;private String eno;private String ename;private String password;public Integer getId() return id;public void setId(Integer id) this.id = id;public String getEno() return eno;public void setEno(Stri
8、ng eno) this.eno = eno;public String getEname() return ename;public void setEname(String ename) this.ename = ename;public String getPassword() return password;public void setPassword(String password) this.password = password;.wtu.cg.dao: 接口IUserDao:package .wtu.cg.dao;import java.util.Co
9、llection;import org.springframework.dao.DataAccessException;import .wtu.cg.domain.Teacher;public interface IUserDao<T> public T findEntityByProperties(String eno, String password, String category)throws DataAccessException;public T findEntityByEno(String eno, String category)throws DataA
10、ccessException;public void saveEntity(T entity) throws DataAccessException;public void deleteEntity(String eno) throws DataAccessException;public Collection<Teacher> findEntityByDepartCode(String departCode)throws DataAccessException;Ueserdao類:package .wtu.cg.dao;import java.util.Collect
11、ion;import java.util.List;import javax.sql.DataSource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.dao.DataAccessException;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;import org.springframewo
12、dparam.BeanPropertySqlParameterSource;import org.springframework.jdbc.core.simple.SimpleJdbcInsert;import org.springframework.stereotype.Repository;import .wtu.cg.domain.BaseEntity;import .wtu.cg.domain.Student;import .wtu.cg.domain.Teacher;Repositorypublic class U
13、serDao<T> implements IUserDao<T> private JdbcTemplate jdbcTemplate;private SimpleJdbcInsert insertEntity;Autowiredpublic void init(DataSource dataSource) jdbcTemplate = new JdbcTemplate(dataSource);insertEntity = new SimpleJdbcInsert(dataSource).usingGeneratedKeyColumns("id");S
14、uppressWarnings("unchecked")Overridepublic T findEntityByProperties(String eno, String password, String category)throws DataAccessException try Class<?> instance = null;String sql = null;if (category.equals("1") instance = Teacher.class;sql = "select * from teacher whe
15、re eno=? and password=?" else if (category.equals("2") instance = Student.class;sql = "select * from student where eno=? and password=?"return (T) jdbcTemplate.queryForObject(sql, new Object eno,password , BeanPropertyRowMapper.newInstance(instance); catch (Exception e) retu
16、rn null;SuppressWarnings("unchecked")Overridepublic T findEntityByEno(String eno, String category)throws DataAccessException String sql;Class<?> cls;if (category.equals("1") sql = "select * from teacher where eno=?"cls = Teacher.class; else sql = "select * fr
17、om student where eno=?"cls = Student.class;List<?> list = jdbcTemplate.query(sql,BeanPropertyRowMapper.newInstance(cls), eno);if (list.size() = 0)return null;return (T) list.get(0);Overridepublic void saveEntity(T register) throws DataAccessException String tableName = null;String sql = n
18、ull;String eno;BaseEntity entity;if (register instanceof Student) eno = (Student) register).getEno();entity = (BaseEntity) findEntityByEno(eno, "2");tableName = "student"sql = "update student set password=?,ename=?,email=?,address=? where eno=?" else eno = (Teacher) reg
19、ister).getEno();entity = (BaseEntity) findEntityByEno(eno, "1");tableName = "teacher"sql = "update teacher set password=?,ename=?,jobtitle=? where eno=?"if (entity = null) if (insertEntity.getTableName() = null)insertEntity.setTableName(tableName);insertEntity.execute(n
20、ew BeanPropertySqlParameterSource(register); else if (tableName.equals("teacher") Teacher teacher = (Teacher) register;jdbcTemplate.update(sql, teacher.getPassword(),teacher.getEname(), teacher.getJobTitle(),teacher.getEno(); else Student student = (Student) register;jdbcTemplate.update(sq
21、l, student.getPassword(),student.getEname(), student.getEmail(),student.getAddress(), student.getEno();Overridepublic void deleteEntity(String eno) throws DataAccessException / TODO Auto-generated method stubOverridepublic Collection<Teacher> findEntityByDepartCode(String departCode)throws Dat
22、aAccessException / TODO Auto-generated method stubreturn null;.wtu.cg.service:userservice類:package .wtu.cg.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import .wtu.cg.dao.IUserDao;import .wtu.cg.domain.Log
23、iner;Servicepublic class UserService<T> implements IUserService<T> private IUserDao<T> userDao;Autowiredpublic void setUserDao(IUserDao<T> userDao) this.userDao = userDao;Overridepublic T findUser(Loginer loginer) return userDao.findEntityByProperties(loginer.getEno(),loginer
24、.getPassword(), loginer.getCategory();Overridepublic void saveUser(T instance) userDao.saveEntity(instance);實現(xiàn)Userservice的接口IUsersseivice類:package .wtu.cg.service;import .wtu.cg.domain.Loginer;public interface IUserService<T> public T findUser(Loginer loginer);public void saveUser(
25、T instance);.wtu.cg.web:homecontroll類:package .wtu.web;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;Controllerpublic class HomeController RequestMapping(value =
26、"/", method = RequestMethod.GET)public String homeHandler() return "home"studentEditForm類:package .wtu.web;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springfra
27、mework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.SessionAttributes;import .wtu.cg.domain.Student;import .wtu.cg.service.IUserService;ControllerSessionAttributes("regstudent")pub
28、lic class StudentEditForm private IUserService<Student> uszerSerice;Autowiredpublic void setUserService(IUserService<Student> userSerice) this.uszerSerice = userSerice;RequestMapping(value = "/student.html", method = RequestMethod.GET)public String teacherHandler() return "
29、;/student"RequestMapping(value = "/students/register.html", method = RequestMethod.POST)public String processSubmit(Student student, Model model) uszerSerice.saveUser(student);model.addAttribute("regstudent", student);return "redirect:/students/register.html"Reques
30、tMapping(value = "/students/sucess.html", method = RequestMethod.GET)public String gotoSucess() return "/students/sucess"TeacherEditForm類:package .wtu.web;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import o
31、rg.springframework.web.bind.annotation.RequestMethod;Controllerpublic class TeacherEditForm RequestMapping(value = "/teacher.html", method = RequestMethod.GET)public String teacherHandler() return "/teacher"Userloginform類:package .wtu.web;import org.springframework.beans.fa
32、ctory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.SessionAttributes;
33、import .wtu.cg.domain.Loginer;import .wtu.cg.domain.Student;import .wtu.cg.domain.Teacher;import .wtu.cg.service.IUserService;ControllerSessionAttributes("loginuser")public class UserLoginForm private IUserService<?> userSerice;Autowiredpublic void setUserSeri
34、ce(IUserService<?> userSerice) this.userSerice = userSerice;RequestMapping(value = "/login.html", method = RequestMethod.GET)public String setupForm() return "/login"RequestMapping(value = "/login.html", method = RequestMethod.POST)public String processSubmit(Logi
35、ner loginer, Model model) String url = "redirect:/login.html"Object o = userSerice.findUser(loginer);if (o instanceof Teacher && (Teacher) o).getEno() != null) url = "redirect:/teacher.html"if (o instanceof Student && (Student) o).getEno() != null)url = "redi
36、rect:/student.html"if (o = null)o = "notfound"model.addAttribute("loginuser", o);return url;Web部分代碼:Service和Servlet配置文件:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="http:
37、//2001/XMLSchema-instance" xmlns:context="/schema/context"xmlns:tx="/schema/tx" xmlns:aop="/schema/aop"xsi:schemaLocation="/schema/aop
38、 /schema/aop/spring-aop-3.0.xsd/schema/beans /schema/beans/spring-beans.xsd/schema/tx /schema/tx/spring-tx-3.0.xsd/schema/cont
39、ext /schema/context/spring-context-3.0.xsd"><!- 掃描Bean,但不掃描Controller -><context:component-scan base-package=".wtu.cg"><context:exclude-filter expression="org.springframework.stereotype.Controller"type="annotation&quo
40、t; /></context:component-scan><!- XML AOP聲明性事務配置 -><aop:config><aop:pointcut id="allServiceMethods"expression="execution(* .wtu.cg.service.*.*(.)" /><aop:advisor advice-ref="defaultTransactionAdvice"pointcut-ref="allServiceMethod
41、s" /></aop:config><!- 設置事務屬性 -><tx:advice id="defaultTransactionAdvice" transaction-manager="dbcp-transactionManager"><tx:attributes><tx:method name="find*" propagation="REQUIRED" read-only="true" /><tx:method
42、 name="save*" propagation="REQUIRED" /><tx:method name="*" propagation="SUPPORTS" read-only="true" /></tx:attributes></tx:advice><!- 配置DBCP連接池數(shù)據(jù)源 -><bean id="dataSource" class="mons.dbcp.BasicDataSource&qu
43、ot;><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url"value="jdbc:mysql:/localhost/test?useUnicode=true&characterEncoding=UTF-8" /><property name="username" value="root" /><
44、property name="password" value="123456" /><property name="initialSize" value="5" /><property name="maxActive" value="10" /></bean><bean id="dbcp-transactionManager"class="org.springframework.jdbc.datas
45、ource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean></beans><?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="http:/
46、/2001/XMLSchema-instance" xmlns:mvc="/schema/mvc"xmlns:context="/schema/context"xsi:schemaLocation="/schema/mvc /schema/mvc/spring-mvc-3.0.xsdht
47、tp://schema/beans /schema/beans/spring-beans.xsd/schema/context /schema/context/spring-context-3.0.xsd"><!- 注解驅動模式 -><mvc:annotation-driven /><!- 處理靜態(tài)資源文件 -><mvc
48、:resources mapping="/images/*" location="/resources/images/" /><!- 啟用對類包進行掃描以實施注釋驅動 Bean定義以及自動注入功能,只掃描Controller -><context:component-scan base-package=".wtu.cg"use-default-filters="false"><context:include-filter expression="org.sp
49、ringframework.stereotype.Controller"type="annotation" /></context:component-scan><!- 視圖解析器 -><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/" /><property
50、 name="suffix" value=".jsp" /></bean><!-注冊基于瀏覽器默認語言的Bean -><bean id="messageSource"class="org.springframework.context.support.ResourceBundleMessageSource"><property name="basename" value="messages" /></bean>
51、<!-定義默認的錯誤頁面 -><beanclass="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"><property name="exceptionMappings"><props><prop key="java.lang.Exception">errors</prop></props></property></bean><
52、/beans>Web的配置文件:<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="/2001/XMLSchema-instance" xmlns:web=" xmlns=" xsi:schemaLocation=" id="WebApp_ID" version="3.0"> <display-name>model2.1</
53、display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/model2.1-service.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listen
54、er-class> </listener> <filter> <filter-name>characterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-v
55、alue> </init-param> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>model2.2</servlet-name> <servlet-class>org.springframework.we
56、b.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/model2.1-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servle
57、t-mapping> <servlet-name>model2.2</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout>10</session-timeout> </session-config> <welcome-file-list> <welcome-file></welcome-file> </
58、welcome-file-list></web-app>各部分界面代碼:Login.jsp代碼:<% page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%taglib prefix="c" uri="<html><head><meta http-equiv="Content-Type" content=&qu
59、ot;text/html; charset=UTF-8"><title>用戶登錄</title></head><body background="resourse/images/m1.jsp"><div align="center"><form method="post">用戶名<input type="text" name="eno" />密碼<input type="passw
60、ord"name="password" /><br> <input type="radio" name="category"value="1" />教師 <input type="radio" name="category" value="2"checked>學生 <br> <input type="submit" value="登錄" /> <input type="reset" value="重填" /></form><c:if test='$!empty loginuser&&loginuser="notfound"'><font style="color:#FF0000">登錄失?。?lt;/font></c:if>&
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 廣州市安全生產(chǎn)許可證延期
- 咖啡廳員工管理規(guī)范與培訓計劃
- 安全生產(chǎn)15條安全措施
- 漁業(yè)安全生產(chǎn)制度
- BIM數(shù)字孿生集成-第1篇-洞察及研究
- 2018年度安全生產(chǎn)工作總結
- 安全生產(chǎn)管理的基本原理
- 安全反思心得體會范文
- 食品安全自查制度表格
- 倉庫保管員安全心得體會
- 吊頂工程施工培訓講義內(nèi)容詳細
- 天門山污水處理廠二期擴建項目環(huán)境影響報告書
- 婦產(chǎn)科學 妊娠合并心臟病
- -衛(wèi)生資格-副高-疾病控制-副高-章節(jié)練習-慢性非傳染性疾病控制-試題(單選題)(共1125題)
- 骨質疏松病人的護理
- 高中英語全國高考考綱詞匯3600匯總
- GB/T 35068-2018油氣管道運行規(guī)范
- GB/T 13277.7-2021壓縮空氣第7部分:活性微生物含量測量方法
- 2023年婁底冷水江市廣播電視臺(融媒體中心)招聘筆試模擬試題及答案解析
- 特勞特戰(zhàn)略定位總裁課程課件
- 陳寶光-TTT課程開發(fā)與設計(講義)V2.1
評論
0/150
提交評論