SpringMVC+Spring3+Hibernate4开发环境搭建

早期的项目比较简单,多是用JSP 、Servlet + JDBC 直接搞定,后来使用 Struts1(Struts2)+Spring+Hibernate, 严格按照分层概念驱动项目开发,这次又使用 Spring MVC取代Struts来进行开发。
MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下SpringMVC+Spring3+Hibernate4的开发环境搭建
先大致看一下项目结构:
SpringMVC+Spring3+Hibernate4开发环境搭建_第1张图片
 
具体的代码不再演示,主要是走了一个很平常的路线,mvc-servcie-dao-hibernate的结构,源码可以下载,主要看一下配置文件。解释见注释
web.xml


  SpringMVC
  
    index.jsp
  
  
   
  
  		contextConfigLocation
  		classpath*:config/spring-*.xml
  
  
  
  
  	 org.springframework.web.context.ContextLoaderListener
  
  
  
  
  
    springMVC
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath*:config/spring-servlet.xml
    
    1
  
  
  
    springMVC
    /
  
  
  
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    
  
  
    encodingFilter
    /*
  
 
  
  
  
  		openSession
  		org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
  
  
  
  	openSession
  	/*
  
  
SpringMVC+Spring3+Hibernate4开发环境搭建_第2张图片
spring-servlet.xml



	
	

	
	
	
	
	

	
	

	
	
	

	
	

	
	
		
		
		
	

	
	

  
spring-hibernate.xml


	
	
	
		
		
		
		
	

	
	
		
		
			
				org.hibernate.dialect.MySQLDialect
				update
				true
				true
			
		
		
			
				
					classpath*:config/hibernate/hibernate.cfg.xml
				
			
		
	

    
	
		
	
    
    
	
		
		
			
				PROPAGATION_REQUIRED,-Exception
				PROPAGATION_REQUIRED,-Exception
				PROPAGATION_REQUIRED,-Exception
				PROPAGATION_REQUIRED,-Exception
				PROPAGATION_REQUIRED,-Exception
				PROPAGATION_REQUIRED,-Exception
				PROPAGATION_NEVER
			
		
	

  

         spring-core.xml


     
    
    
	
   

         spring-user.xml


]>


    
	
		
	
	
	
		
	
	
	
	
		
	
	


           hibernate.cfg.xml



    
        
        
    

下面再来看看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(HttpServletRequest request){
		
		List userList=userManager.getAllUser();
		
		request.setAttribute("userlist", userList);
		
		return "/UserMain";
	}
	
}


源码下载……
下面还会介绍一些Struts2与SpringMVC的区别

 
 

你可能感兴趣的:(Linux,J2ee,Hibernate,Spring)