标签:
前面我们已整合了spring + hibernate,并建立了个用户的增加的实例,通过了单元测试,能正常增加数据。今天我们就来集成spring mvc以便建立web界面来输入数据并提交,后台再保存入库。关于spring mvc的一些基础理论及使用方法,网上有很多的资料,我这里就不多说了,下面我们进入实战。
<!-- 工程显示名称 -->
<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*:applicationContext*.xml
</param-value>
</context-param>
<!-- webApp的根,用来区分部署到同一tomcat的不同应用程序 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>extdesktop.webapp.root</param-value>
</context-param>
<!-- 配置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>
<?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>
<?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>
<%@ 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>
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 整合(三)
标签:
原文地址:http://www.cnblogs.com/ympcsoft/p/5244196.html