多平台统一管理软件接口,如何实现多平台统一管理软件接口
182
2023-06-20
浅谈SpringMVC+Spring3+Hibernate4开发环境搭建
早期的项目比较简单,多是用jsP 、Servlet + JDBC 直接搞定,后来使用 Struts1(Struts2)+Spring+Hibernate, 严格按照分层概念驱动项目开发,这次又使用 Spring MVC取代Struts来进行开发。
MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下SpringMVC+Spring3+Hibernate4的开发环境搭建
先大致看一下项目结构:
具体的代码不再演示,主要是走了一个很平常的路线,mvc-servcie-dao-hibernate的结构,源码可以下载,主要看一下配置文件。解释见注释
web.xml
spring-servlet.xml
xmlns:context="http://springframework.org/schema/context" xmlns:p="http://springframework.org/schema/p" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd"> class="org.springframework.web.servlet.view.InternalResourceViewResolver">
xmlns:context="http://springframework.org/schema/context" xmlns:p="http://springframework.org/schema/p"
xmlns:mvc="http://springframework.org/schema/mvc" xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.0.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
spring-hibernate.xml
xmlns:context="http://springframework.org/schema/context" xmlns:p="http://springframework.org/schema/p" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd"> class="org.springframework.jdbc.datasource.DriverManagerDataSource"> class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> classpath*:config/hibernate/hibernate.cfg.xml class="org.springframework.orm.hibernate4.HibernateTransactionManager"> class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true"> http://
xmlns:context="http://springframework.org/schema/context" xmlns:p="http://springframework.org/schema/p"
xmlns:mvc="http://springframework.org/schema/mvc" xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.0.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> classpath*:config/hibernate/hibernate.cfg.xml
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
classpath*:config/hibernate/hibernate.cfg.xml
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true"> http://
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
http://
spring-core.xml
xmlns:context="http://springframework.org/schema/context" xmlns:p="http://springframework.org/schema/p" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">
xmlns:context="http://springframework.org/schema/context"
xmlns:p="http://springframework.org/schema/p"
xmlns:mvc="http://springframework.org/schema/mvc"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.0.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">
spring-user.xml
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>
hibernate.cfg.xml
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
下面再来看看Controller
package com.jialin.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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 com.jialin.entity.User;
import com.jialin.service.IUserManager;
@Controller //类似Struts的Action
@RequestMapping("/user")
public class UserController {
@Resource(name="userManager") // 获取spring配置文件中bean的id为userManager的,并注入
private IUserManager userManager;
@RequestMapping("/addUser") // 请求url地址映射,类似Struts的action-mapping
public String addUser(User user){
if(userManager.addUser(user))
{
// 重定向
return "redirect:/user/getAllUser";
}else
{
return "/fail";
}
}
@RequestMapping("/updateUser")
public String updateUser(User user,HttpServletRequest request){
if (userManager.updateUser(user))
{
user = userManager.getOneUser(user);
request.setAttribute("user", user);
return "/UserEdit";
}else
{
return "/fail";
}
}
@RequestMapping("/delUser")
public void delUser(User user,HttpServletResponse response){
String result = "{\"result\":\"error\"}";
if(userManager.delUser(user)){
result = "{\"result\":\"success\"}";
}
PrintWriter out = null;
response.setContentType("application/json");
try {
out = response.getWriter();
out.write(result);
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping("/toAddUser")
public String toAddUser(){
return "/UserAdd";
}
@RequestMapping("/toUpdateUser")
public String toUpdateUser(User user,HttpServletRequest request){
User user1=userManager.getOneUser(user);
request.setAttribute("user1", user1);
return "/UserEdit";
}
@RequestMapping("/getAllUser")
public String getAllUser(HttpSeDHMZOyWckrvletRequest request){
List userList=userManager.getAllUser();
request.setAttribute("userlist", userList);
return "/UserMain";
}
}
源码下载……
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~