eclipse(springmvc+spring+hibernate)
来源:互联网 发布:雪梨开的淘宝店网址 编辑:程序博客网 时间:2024/06/02 09:53
eclipse(springmvc+spring+hibernate)
案例下载:SSH2
整合包:springmvc+spring+hibernate
1、右键新建web project
(需要生成web.xml文件)
2、修改web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 载入spring配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- spring监听器 --> <listener> <description>spring监听器</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 字符过滤器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring mvc servlet *.do --> <servlet> <description>spring mvc servlet</description> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring mvc 配置文件</description> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <display-name></display-name></web-app>
3、添加配置
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-autowire="byName"> <!-- 开启注解 --> <context:annotation-config /> <!-- spring 扫描路径,注意当前工程只需要扫描dao和service,srpingmvc或者struts2注解才有变化 --> <context:component-scan base-package="com.qzly.dao,com.qzly.service" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/gxsoft"> </property> <property name="username" value="root"></property> <property name="password" value="123"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> <property name="mappingResources"> <list> <value>com/qzly/po/User.hbm.xml</value> </list> </property> </bean> <!-- 配置声明式事务管理(采用注解的方式) --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="txManager"/></beans>
log4j.properties
#\u8bbe\u7f6e\u7ea7\u522b\u548c\u591a\u4e2a\u76ee\u7684\u5730log4j.rootLogger=INFO,appender1,appender2#\u8f93\u51fa\u5230\u63a7\u5236\u53f0log4j.appender.appender1=org.apache.log4j.ConsoleAppender#\u8bbe\u7f6e\u8f93\u51fa\u6837\u5f0flog4j.appender.appender1.layout=org.apache.log4j.TTCCLayout#\u8f93\u51fa\u5230\u6587\u4ef6(\u8fd9\u91cc\u9ed8\u8ba4\u4e3a\u8ffd\u52a0\u65b9\u5f0f)log4j.appender.appender2=org.apache.log4j.FileAppender#\u8bbe\u7f6e\u6587\u4ef6\u8f93\u51fa\u8def\u5f84#\u30101\u3011\u6587\u672c\u6587\u4ef6log4j.appender.appender2.File=d:/fangjian.log#\u30102\u3011HTML\u6587\u4ef6#log4j.appender.appender2.File=c:/Log4JDemo02.html#\u8bbe\u7f6e\u6587\u4ef6\u8f93\u51fa\u6837\u5f0flog4j.appender.appender2.layout=org.apache.log4j.TTCCLayout#log4j.appender.appender2.layout=org.apache.log4j.HTMLLayoutlog4j.logger.jdbc.sqlonly=DEBUG,sqllog4j.additivity.jdbc.sqlonly=truelog4j.appender.sql=org.apache.log4j.ConsoleAppenderlog4j.appender.sqlThreshold=debuglog4j.appender.sqlTarget=System.outlog4j.appender.sqlEncoding=GBKlog4j.appender.sql.layout=org.apache.log4j.PatternLayoutlog4j.appender.sql.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 日期转换 必须放在<mvc:annotation-driven />前面 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> <!-- 注解方式 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 自动扫描且只扫描@Controller和@Component(为以后 aop准备) --> <context:component-scan base-package="com.qzly.web"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Component" /> </context:component-scan> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix" value="/WEB-INF/jsp"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
4、添加目录结构并写入内容
5、添加jsp
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="<%=path %>/userController/login.do" method="post"> <table> <tr> <td>用户名</td> <td><input type="text" name="ename"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="password"></td> </tr> <tr> <td colspan="2"><input type="submit" value="立即登陆"></td> </tr> </table> </form> </body></html>
add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="<%=path %>/userController/add.do" method="post"> <table> <tr> <td>用户名</td> <td><input type="text" name="ename" value=""></td> </tr> <tr> <td>密码</td> <td><input type="text" name="password" value=""></td> </tr> <tr> <td>所属部门</td> <td><input type="text" name="dept" value=""></td> </tr> <tr> <td>年龄</td> <td><input type="text" name="age" value=""></td> </tr> <tr> <td>性别</td> <td><input type="text" name="gender" value=""></td> </tr> <tr> <td>入职时间</td> <td><input type="text" name="workDate" value=""></td> </tr> <tr> <td><input type="submit" value="添加"></td> <td><input type="button" value="返回" onclick="history.back(-1);"></td> </tr> </table> </form> </body></html>
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div> <input type="button" onclick="window.location.href='<%=path %>/userController/list.do'" value="查询"> <input type="button" onclick="window.location.href='<%=path %>/userController/toadd.do'" value="添加"> </div> <table> <thead> <tr> <th>编号</th> <th>姓名</th> <th>密码</th> <th>所属部门</th> <th>年龄</th> <th>性别</th> <th>入职日期</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach items="${users }" var="items"> <tr> <td>${items.eid }</td> <td>${items.ename }</td> <td>${items.password }</td> <td>${items.dept }</td> <td>${items.age }</td> <td>${items.gender }</td> <td><fmt:formatDate value="${items.workDate }" pattern="yyyy-MM-dd"/></td> <td> <a href="<%=path %>/userController/toupdate.do?eid=${items.eid}">修改</a>/ <a href="<%=path %>/userController/delete.do?eid=${items.eid}">删除</a> </td> </tr> </c:forEach> </tbody> </table> </body></html>
update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="<%=path %>/userController/update.do" method="post"> <input type="hidden" name="eid" value="${vo.eid }"> <table> <tr> <td>用户名</td> <td><input type="text" name="ename" value="${vo.ename }"></td> </tr> <tr> <td>密码</td> <td><input type="text" name="password" value="${vo.password }"></td> </tr> <tr> <td>所属部门</td> <td><input type="text" name="dept" value="${vo.dept }"></td> </tr> <tr> <td>年龄</td> <td><input type="text" name="age" value="${vo.age }"></td> </tr> <tr> <td>性别</td> <td><input type="text" name="gender" value="${vo.gender }"></td> </tr> <tr> <td>入职时间</td> <td> <input type="text" name="workDate" value="<fmt:formatDate value="${vo.workDate }" type="both" pattern="yyyy-MM-dd" />"> </td> </tr> <tr> <td><input type="submit" value="修改"></td> <td><input type="button" value="返回" onclick="history.back(-1);"></td> </tr> </table> </form> </body></html>
6、乱码解决
applicationContext.xml
添加
?useUnicode=true&characterEncoding=utf-8
框架搭建成功!
0 0
- eclipse(springmvc+spring+hibernate)
- eclipse搭建SSH框架(Spring+SpringMVC+Hibernate)
- SpringMVC+Spring+Hibernate整合
- spring + springMVC + hibernate 集成
- SpringMVC整合Spring、Hibernate
- Spring-SpringMVC-Hibernate整合
- 搭建springMVC+Hibernate+Spring
- SpringMVC+Spring+Hibernate整合
- SpringMVC+Spring+Hibernate+EHCache
- SpringMVC+Spring+Hibernate配置
- springMVC+spring+hibernate配置
- SpringMVC+Spring+hibernate整合
- ssm Springmvc+spring+hibernate
- springmvc-spring-hibernate整合
- spring+springmvc+hibernate crud
- Spring SpringMVC Hibernate 注解
- hibernate+spring+springMVC
- spring+springmvc+hibernate
- executemany存万得数据到数据库
- XML简介&DOM、PULL、SAX解析对比
- springMVC web.xml 中配置加载顺序
- 网站seo
- 剑指offer
- eclipse(springmvc+spring+hibernate)
- Android中TextView内容过长 添加滚动条进行展示
- 读书笔记--工厂方法模式
- Plug-in Item控件
- 剑指offer 序列化二叉树
- C++第五次作业
- javascript- 身份证校验
- c++之stack
- spring过滤空指针