spring mvc+spring + hibernate 整合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring mvc+spring + hibernate 整合相关的知识,希望对你有一定的参考价值。
前面我们已整合了spring + hibernate,并建立了个用户的增加的实例,通过了单元测试,能正常增加数据。今天我们就来集成spring mvc以便建立web界面来输入数据并提交,后台再保存入库。关于spring mvc的一些基础理论及使用方法,网上有很多的资料,我这里就不多说了,下面我们进入实战。 因为我们项目会用到很多的图片、js代码、css样式文件等。我们在webapp目录下建立个static目录,统一对这些文件进行管理
一:web.xml配置
它提供了我们应用程序的配置设定。内容包括有:
.站台的名称和说明
.针对环境参数(Context)做初始化工作
.Servlet的名称和映射
.Session的设定
.Tag library的对映
.JSP网页设定
.Mime Type处理
.错误处理
.利用JDNI取得站台资源
要了解web.xml的设定值,必须了解它的schema,从web.xml中知道它的schema是由Sum Microsystems公司定制的,我们可以通过http://java.sun.com/xml/ns/j2ee/web-mapp_2_4.xsd网页来详细了 解。这里我只说说我们集成spring mvc需要的基本配置
1.配置应用的基本信息
<!-- 工程显示名称 -->
<display-name>ExtDesktop</display-name>
<!-- 工程的描述性说明 -->
<description>Ext Js Desktop Web APP 学习</description>
<!-- 工程的图标定义 -->
<icon>
<small-icon>/images/small.gif</small-icon>
<large-icon>/images/large.gir</large-icon>
</icon>
2.ServletContext(上下文)
<!-- 配置ServletContext(上下文) -->
<!-- 配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext*.xml
</param-value>
</context-param>
<!-- webApp的根,用来区分部署到同一tomcat的不同应用程序 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>extdesktop.webapp.root</param-value>
</context-param>
3.配置springmvc拦截器
<!-- 配置springmvc 拦截器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
4.完整的web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="extdesktop"
version="3.0"
metadata-complete="true">
<!-- 工程显示名称 -->
<display-name>ExtDesktop</display-name>
<!-- 工程的描述性说明 -->
<description>Ext Js Desktop Web APP 学习</description>
<!-- 工程的图标定义 -->
<icon>
<small-icon>/images/small.gif</small-icon>
<large-icon>/images/large.gir</large-icon>
</icon>
<!-- 配置ServletContext(上下文) -->
<!-- 配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring*.xml
</param-value>
</context-param>
<!-- webApp的根,用来区分部署到同一tomcat的不同应用程序 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>extdesktop.webapp.root</param-value>
</context-param>
<!-- Spring配置 -->
<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>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字符过滤 -->
<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-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 默认跳转到根目录下的页面,在这里为login.jsp -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
二:springmv-servlet.xml配置
springmvc-servlet这个名字是因为上面web.xml中<servlet-name>标签配的值为springmvc(<servlet-name>spring</servlet-name>),再加上“-servlet”后缀而形成的springmvc-servlet.xml文件名,如果改为其它名称(如spring),对应的文件名则为【名称】-servlet.xml(如spring-servlet)。
<?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:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.ympcsoft" />
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 配置静态文件访问 -->
<mvc:resources location="/static/" mapping="/static/**"/>
<!-- 视图解释类 -->
<bean
id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property
name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property
name="prefix"
value="/WEB-INF/view/" />
<property
name="suffix"
value=".jsp" />
</bean>
</beans>
三:login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="login/login" method="post">
用户名:<input type="text" name="userName"/>
密码:<input type="text" name="userPwd"/><br>
<input type="radio" name="userCh" value="portal" checked>经典界面
<input type="radio" name="userCh" value="desktop">类桌面界面
<input type="submit" value="提交"/>
</form>
</body>
</html>
四:loginController.java
package com.ympcsoft.extdesktop.controller.login;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.ympcsoft.extdesktop.domain.sys.SysUser;
import com.ympcsoft.extdesktop.service.sys.SysUserService;
@Controller
@RequestMapping("/login")
public class LoginController{
@Resource
private SysUserService sysUserService;
@RequestMapping("/login")
public ModelAndView login(SysUser sysUserModel, HttpServletRequest request, HttpServletResponse response) throws Exception {
String userName = sysUserModel.getUserName();
String userPwd = sysUserModel.getUserPwd();
String userCh = sysUserModel.getUserCh();
ModelAndView mav = null;
System.out.println("user:"+userName + " userPwd:" + userPwd + " userCh:" + userCh);
//sysUser持久化到数据库中
SysUser sysUser = new SysUser();
sysUser.setUserName(sysUserModel.getUserName());
sysUser.setUserPwd(sysUserModel.getUserPwd());
sysUser.setUserCh(sysUserModel.getUserCh());
sysUserService.addUser(sysUser);
if ("portal".equals(userCh)){
mav = new ModelAndView("/desktop/portal");
mav.addObject("常规界面", " hello "+userName);
} else {
mav = new ModelAndView("/desktop/desktop");
mav.addObject("desktop界面", " hello "+userName);
}
return mav;
}
}
以上是关于spring mvc+spring + hibernate 整合的主要内容,如果未能解决你的问题,请参考以下文章
Spring MVC学习笔记---Spring MVC 的HelloWorld
spring mvc中 Circular view path 问题