最近要做一个Web项目,采用了Spring的MVC框架,特此记录下这次搭建过程

1.准备Spring所需的MVC,IOC,数据库等Jar包:

Spring3.2 MVC框架搭建_第1张图片

spring配置文件:

wKioL1TCDWDyMlBrAABQQKqI53I159.jpg


2.Web.xml文件

  配置拦截请求DispatcherServlet,上下文监听器SpringContext,过滤器Filter

  
  
    DtServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      /WEB-INF/classes/*-mvc.xml
    
    1
  
  
    DtServlet
    *.do
  
  
    customDim.html
    index.jsp
  
  
  
  
    
			org.springframework.web.context.ContextLoaderListener
		
  
  
    contextConfigLocation
    classpath:spring-context.xml
  
  
  
  
    
    characterEncodingFilter  
    org.springframework.web.filter.CharacterEncodingFilter  
      
        encoding  
        UTF-8  
      
      
        forceEncoding  
        true  
      
    
    
      characterEncodingFilter  
      /*  
  


3. spring-context.xml上下文监听器文件配置:IOC对象初始化

     
    
      
          
          
          
        
        
        
        
        
      
  
          
      
    
     
        
    
    
    
        
    

4. jdbc.properties文件

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1526:testdb
jdbc.username=test001
jdbc.password=123456
jdbc.maxActive=10
jdbc.maxIdle=3
jdbc.maxWait=5
jdbc.defaultAutoCommit=false


5.spring-mvc.xml中MVC拦截后转发配置controller










6.MVC中Conntroller类

  通过spring-mvc.xml文件中配置拦截的请求都转给com.sun.dt.conntroller类处理,类中可针对不同不同URL请求给不同的conntroller类和其方法处理

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/data/*")
public class DataController {

	@Resource(name = "dataService") 
	private DataService dataService;
	
	private  String username = "test001";
	
	private String code = "";

	//获取维度机构信息
	@RequestMapping(value="department",method=RequestMethod.GET) 
    public  @ResponseBody List department(){  
		ArrayList list = dataService.getDepartmentInfo(code);
        return list;  
    } 
	
	
	@RequestMapping(value="catgory1",method=RequestMethod.GET) 
    public void catgory11(String username,HttpServletResponse response){  
	 
	    String jsonString = dataService.getCategoryInfo(username);
	    try {
		    response.setCharacterEncoding("UTF-8"); 
	        response.setContentType("text/json");   
			PrintWriter out = response.getWriter();
	        out.print(jsonString); 
	        out.flush();  
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    } 
	
	//获取用户自定义维度信息
	@RequestMapping(value="catgory",method=RequestMethod.GET) 
    public void catgory(PrintWriter printWriter){  
	 
	    String jsonString = dataService.getCategoryInfo(umName);
        printWriter.write(jsonString); 
	    printWriter.flush();  
	    
    } 
	
	
	// 用户自定义维度保存接口
	@RequestMapping(value="dimdef",method=RequestMethod.POST) 
    public @ResponseBody Map dimdef(@RequestParam("json")String json){  
		HashMap map = new HashMap();
		
		System.out.println(json);
		if(checkJson(json)){
			dataService.saveDefDimension(umName, json);
			map.put("result", "defined dimension finished");
			map.put("code","1");
			
		}else{
			map.put("result", "json invalid");
			map.put("code","-1");
		}
		return map;
    } 
	
	
	// 用户树形结构书签保存接口
	@RequestMapping(value="marksaving",method=RequestMethod.POST) 
    public @ResponseBody Map saveBookMark(@RequestParam("bookmark")String json){  
		HashMap map = new HashMap();
		System.out.println(json);
		if(checkJson(json)){
			dataService.saveBookMark(umName, json);
			//System.out.println(json);
			map.put("result", "defined dimension finished");
			map.put("code","1");
			
		}else{
			map.put("result", "json invalid");
			map.put("code","-1");
		}
		return map;
    } 
	
	// 用户获取书签获取书签列表信息接口
	@RequestMapping(value="marklist",method=RequestMethod.GET) 
    public  @ResponseBody List bookMarkList(){  
		ArrayList list = dataService.getBookMarkList(umName);
        return list;  
    } 
	
	// 用户获取书签节点信息接口
	@RequestMapping(value="bookmark",method=RequestMethod.GET) 
    public void bookMark(PrintWriter printWriter,@RequestParam("markid")String markId){  
		BookMark bm = new BookMark();
		bm.setMarkId(markId);
		bm.setUmName(umName);
	    String jsonString = dataService.getBookMarkInfo(bm);
        printWriter.write((jsonString == null) ? "" : jsonString); 
	    printWriter.flush();  
	    
    } 
	
	// 用户书签删除接口markid=11
	@RequestMapping(value="markdel",method=RequestMethod.GET) 
    public @ResponseBody Map delBookMark(@RequestParam("markid")String markId){  
        HashMap map = new HashMap();
		System.out.println("markdel " + markId);
		if(!markId.equals("")){
			dataService.delBookMark(markId);
			//System.out.println(json);
			map.put("result", "defined dimension finished");
			map.put("code","1");
			
		}else{
			map.put("result", "json invalid");
			map.put("code","-1");
		}
		return map;
    } 
	
	// 用户书签删除接口markid=11
	@RequestMapping(value="markupdate",method=RequestMethod.POST) 
    public @ResponseBody Map updateBookMark(@RequestParam("bookmark")String json){
		HashMap map = new HashMap();
		System.out.println(json);
		if(checkJson(json)){
			dataService.updateBookMark(umName, json);
			//System.out.println(json);
			map.put("result", "defined dimension finished");
			map.put("code","1");
			
		}else{
			map.put("result", "json invalid");
			map.put("code","-1");
		}
		return map;
    } 
	
	private boolean checkJson(String json){
		return true;
	}
	

}

7.浏览器中直接输入

wKioL1TCIRmQg9-_AABWt749LI8864.jpg

即可看见获取机构返回信息



参考:http://blog.csdn.net/swingpyzf/article/details/8904205