变更更正(2014-05-30 13:47:22):一些IDE在web.xml我们会报告这个错误:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param'. One of '{"http://

 xmlns.jcp.org/xml/ns/javaee":enabled, "http://xmlns.jcp.org/xml/ns/javaee":async-supported, "http://

 xmlns.jcp.org/xml/ns/javaee":run-as, "http://xmlns.jcp.org/xml/ns/javaee":security-role-ref, "http://

 xmlns.jcp.org/xml/ns/javaee":multipart-config}' is expected.

你能够无视它,可是假设认为不舒服的话。能够将<load-on-startup>1</load-on-startup>放到<init-param>这个节点的后面

前话:

写了半年的Flex。也就是一个做一个WEB的视频监控浏览端,已经初步成型,如今是要做一个管理平台,于是最终又要用回JAVA了。可是一切都变的陌生了,

比方写个方法或者定义一个变量,总是会用Flex的语法去写,曾经都是用Struts2 Hibernate Spring框架的,如今据说springMVC更流行了。于是花了这一周时间入了下门,感觉

确实不错,我是看的这个系列教程,跟开涛学SpringMvc http://jinnianshilongnian.iteye.com/category/231099,写的非常赞。通俗易懂,非常感谢。

后来顺便也看了下这个前端开发框架Bootstrap也非常不错,简直是后端开发者的福音啊。而且我发现改版后的CSDN登录界面也用到了这个框架,所以学习了下,也是在

一个系列博客中看的。http://www.cnblogs.com/aehyok/p/3404867.html#title02 事实上是跟Bootstrap中文网的教程差点儿相同。可是在博客中看比較集中,而且文件夹清晰,当时在中文网看的12栅格布局是真心没看懂。后来在这位仁兄的博

客中看懂了。在这里很感谢!

今天周五,所以简单的整合了一下来熟悉这周看的东西

老规矩(有图有真相),先看下相关效果图吧:

登录界面:(Bootstrap的样式还挺赞的吧)

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc29uZ2FubGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

主页面:

加入:

改动:(上面的改动和删除你要点击一下某一行。才干进行操作)

项目文件夹结构:

部分配置和相关代码:

web.xml

<?

xml version="1.0" encoding="UTF-8"?

>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="Bufoon_WebApp_ID" version="3.1">
<display-name>WebApp</display-name> <!-- log4j配置。假设不须要以下的配置,能够将log4j文件放到src文件夹下,log4j会默认载入 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/properties/log4j.properties</param-value>
</context-param> <context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener> <!-- 加入对spring的支持 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/xml/applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 加入springMVC前端控制器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<!-- 加入配置文件。假设不想要厦门的配置,能够将 [servlet-name]-servlet.xml放到WEB-INF文件夹下。springMVC会默认载入 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/xml/springMVC-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- 加入UTF-8编码支持 -->
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 当hibernate+spring配合使用的时候。假设设置了lazy=true,那么在读取数据的时候。当读取了父数据后。 hibernate会自己主动关闭session,这样,当要使用子数据的时候,系统会抛出lazyinit的错误。
这时就须要使用spring提供的 OpenSessionInViewFilter,OpenSessionInViewFilter主要是保持Session状态
知道request将所有页面发送到client。这样就能够解决延迟载入带来的问题 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.do,*.action</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>

springMVC-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 注解扫描包 -->
<context:component-scan base-package="com.bufoon.controller" /> <!-- 开启注解 -->
<mvc:annotation-driven /> <!-- 静态资源的訪问 -->
<mvc:resources location="/plugins/" mapping="/plugins/**" /> <!-- 返回JSON模版 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<!-- 注意:FreeMarker与SpringMVC集成时,其配置一定要放在SpringMVC的配置之前 -->
<!-- 否则:在訪问"/getUser"时server就会报告[HTTP Status 404 - /WEB-INF/jsp/myfree.jsp] -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 指定FreeMarker模板文件文件夹 -->
<property name="templateLoaderPath" value="/WEB-INF/app/ftl/" />
<!-- 编码设置 -->
<property name="freemarkerSettings">
<props>
<prop key="defaultEncoding">UTF-8</prop>
</props>
</property>
</bean>
<bean id="freeMarkerViewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="prefix" value="" />
<property name="suffix" value=".ftl" />
<property name="contentType" value="text/html; charset=UTF-8" />
</bean> <!-- 定义视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/app/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

UserController.java

package com.bufoon.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView; import com.bufoon.entity.User;
import com.bufoon.model.UserModel;
import com.bufoon.service.UserService;
import com.bufoon.util.Constants;
import com.bufoon.util.Util;
import com.bufoon.util.page.PageResultSet; @Controller
@RequestMapping("/user")
public class UserController { @Resource
private UserService userService; @RequestMapping("/userLogin")
public String userLogin(UserModel userModel, Model model){
userModel.setPassword(Util.encryptMD5(userModel.getPassword()));
User user = userService.findUserByByCondition(userModel);
if (user == null) {
model.addAttribute("errorInfo", "you password is error!");
return "login";
} Constants.LOGIN_USER = user.getUsername();
return "redirect:/user/index";
}
@RequestMapping("/addUser")
public String addUser(UserModel userModel){
Date date = new Date();
User user = new User();
BeanUtils.copyProperties(userModel, user); //实体属性复制,将userModel中的属性值拷贝到User中
user.setPassword(Util.encryptMD5(userModel.getPassword()));
user.setCreateTime(date);
user.setUpdateTime(date);
user.setUpdateUser(Constants.LOGIN_USER);
user.setCreateUser(Constants.LOGIN_USER);
userService.saveUser(user);
return "redirect:/user/index";
}
@RequestMapping("/index")
public ModelAndView index(UserModel userModel, HttpServletRequest request) {
if(Util.isNull(Constants.LOGIN_USER)){
ModelAndView mav = new ModelAndView("login");
mav.addObject("errorInfo", "user is overdue!");
return mav;
}
return new ModelAndView("userList");
} @RequestMapping("/getUser")
public void getUser(HttpServletRequest request, HttpServletResponse response, Model model) {
int id = Integer.parseInt(request.getParameter("userId"));
User user = userService.getUserById(id);
Map<String, Object> map = new HashMap<String, Object>();
map.put("user", user);
model.addAttribute("user", user);
} @RequestMapping("/updateUser")
public String updateUser(UserModel userModel){
Date date = new Date();
User user = userService.getUserById(userModel.getId());
if (Util.isNull(userModel.getPassword())) {
userModel.setPassword(user.getPassword());
}else {
userModel.setPassword(Util.encryptMD5(userModel.getPassword()));
}
userModel.setCreateTime(user.getCreateTime());
userModel.setUpdateTime(date);
userModel.setCreateUser(Constants.LOGIN_USER);
userModel.setUpdateUser(Constants.LOGIN_USER);
BeanUtils.copyProperties(userModel, user);
userService.updateUser(user);
return "redirect:/user/index";
} @RequestMapping("/deleteUser")
public String deleteUser(HttpServletRequest request){
int id = Integer.parseInt(request.getParameter("userId"));
User user = userService.getUserById(id);
userService.deleteUser(user);
return "redirect:/user/index";
} @RequestMapping("/exit")
public String exit(){
Constants.LOGIN_USER = null;
return "redirect:/";
}
//返回json
@RequestMapping("/getJsonUserList")
@ResponseBody
public ModelMap getJsonUserList(UserModel userModel, HttpServletRequest request, HttpServletResponse response) {
ModelMap modelMap = new ModelMap();
response.setHeader("Access-Control-Allow-Origin", "*");
String sPage = request.getParameter("page");
int page = 1;
if (!Util.isNull(sPage)) {
page = Integer.parseInt(sPage);
}
PageResultSet<User> userPageResult = userService.findPageUserList(userModel, page, Constants.PAGE_SIZE); modelMap.addAttribute("event",userPageResult.getList());
modelMap.addAttribute("pageBean", userPageResult.getPage());
modelMap.addAttribute("pageCount",userPageResult.getPage().getTotalPage());
return modelMap;
} }

代码还是挺多的。不贴了

DEMO源代码到这里下载吧:http://download.csdn.net/detail/soanl/7391345

说明:在项目的doc下有数据库文件。

新建数据库名为 db_shf,编码设为utf-8,然后导入即可

执行 http://你的ip:端口port/WebApp  username:admin,password:admin

下周開始把mina学习下。由于公司项目之间的通信都是用socket通信的,在这个管理平台也要用到socket同主server通信

http://blog.csdn.net/songanling/article/details/26732977 by 布冯(Bufoon)

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Spring4 SpringMVC Hibernate4 Freemaker 集成示例的更多相关文章

  1. Spring4 SpringMVC Hibernate4 Freemaker 整合样例

    更正改动(2014-05-30 13:47:22):有的IDE中web.xml会报这个错: cvc-complex-type.2.4.a: Invalid content was found star ...

  2. spring4+springmvc+hibernate4 demo

    来自 CSDN . 其实下面的更好:加入了maven集成.Spring4 MVC Hibernate4集成 下面也是一篇讲的很详细的文章: hibernate4无法保存数据 而自己遇到的hiberna ...

  3. Spring4+SpringMVC+Hibernate4整合入门与实例

    配置web.xml <? xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&q ...

  4. Spring4 MVC Hibernate4 maven集成

    http://www.cnblogs.com/leiOOlei/p/3727859.html

  5. 基于全注解的SpringMVC+Spring4.2+hibernate4.3框架搭建

    概述 从0到1教你搭建spring+springMVC+hibernate整合框架,基于注解. 本教程框架为基于全注解的SpringMVC+Spring4.2+hibernate4.3,开发工具为my ...

  6. Spring4 MVC Hibernate4集成 Annotation

    Spring4 MVC Hibernate4集成 Annotation 一.本文所用环境 二.工程目录 三.Maven添加依赖 四.新建数据库表 五.配置文件 六.Model层 七.DAO层 八.Se ...

  7. Spring4 MVC Hibernate4集成

      Spring4 MVC Hibernate4集成 一.    本文所用环境 Spring4.0.3.RELEASE Hibernate4.3.5.Final Mysql 二.    工程目录 三. ...

  8. 基于Struts2,Spring4,Hibernate4框架的系统架构设计与示例系统实现

    笔者在大学中迷迷糊糊地度过了四年的光景,心中有那么一点目标,但总感觉找不到发力的方向. 在四年间,尝试写过代码结构糟糕,没有意义的课程设计,尝试捣鼓过Android开发,尝试探索过软件工程在实际开发中 ...

  9. spring4+springmvc+springdataJPA+hibernate4+Junit4整合懒加载问题

    文章目录 技术交流 #摘要 本文主要是为了解决"spring4+springmvc+springdataJPA+hibernate4+junit4整合",注解了OneToMany. ...

随机推荐

  1. WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体

    原文:WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体 运行结果: 事实上很简单,定义好一个正方体,处理好纹理.关于MeshGeometry3D的正确定义和纹理这里就不多讲 ...

  2. 如何:使用 Visual Studio 中的一键式发布来部署 Web 应用程序项目

    原文: 如何:使用 Visual Studio 中的一键式发布来部署 Web 应用程序项目 本主题介绍如何在以下产品中使用 一键式发布 发布(部署)Web 应用程序项目: Visual Studio ...

  3. zookeeper错误KeeperErrorCode = ConnectionLoss解决

    原因: 一般是由于连接还未完成就执行zookeeper的get/create/exsit操作引起的. 解决方法: 利用"CountDownLatch 类 + zookeeper的watche ...

  4. JComboBox

    package swing.combox; import java.awt.FlowLayout; import javax.swing.DefaultComboBoxModel; import ja ...

  5. OpenJDK 阅读源代码 Java 实现字节流输入类

    Java 的输入输出总是给人一种非常混乱的感觉.要想把这个问题搞清楚.必须对各种与输入输出相关的类之间的关系有所了解. 仅仅有你了解了他们之间的关系.知道设计这个类的目的是什么.才干更从容的使用他们. ...

  6. [LeetCode238]Product of Array Except Self

    题目: Given an array of n integers where n > 1, nums, return an array output such that output[i] is ...

  7. MAC 10.10 apache 服务器配置

    mac中自带了apache服务器, 如果需要在mac上使用apache服务器, 只需要配置并启动服务器即可. mac 10.10 中自带的apache版本是 2.4 mac 10.9 中自带的apac ...

  8. Win7 补丁装不上怎么办?

    检查系统盘文件系统是否出错: 打开管理员命令行程序(Win+R,输入 cmd 确认) 如果系统盘是 C 盘,命令行窗口输入 chkdsk C: /f 对提示输入’y’ 重启等待 系统文件检查: 打开管 ...

  9. 【原创】构建高性能ASP.NET站点之一 剖析页面的处理过程(前端)

    原文:[原创]构建高性能ASP.NET站点之一 剖析页面的处理过程(前端) 构建高性能ASP.NET站点之一 剖析页面的处理过程(前端) 前言:在对ASP.NET网站进行优化的时候,往往不是只是懂得A ...

  10. 一个非常有用的函数——COALESCE

    原文:一个非常有用的函数--COALESCE 很多人知道ISNULL函数,但是很少人知道Coalesce函数,人们会无意中使用到Coalesce函数,并且发现它比ISNULL更加强大,其实到目前为止, ...