SpringMVC学习-基础环境搭建

1.下载maven,解压,将bin加入环境变量

2.下载tomcat,解压,运行。

3.打开eclipse,新建maven project

4.next选择maven-archetype-webapp,完成

5.修改WEB-INF下web.xml

注意spring-servlet.xml名字




  Archetype Created Web Application
  
          spring
          org.springframework.web.servlet.DispatcherServlet
          
            contextConfigLocation
            classpath:spring-servlet.xml
        
          
      
 
     
         spring
         /
     


6.在resource下新建文件spring-servlet.xml

注意扫描位置com.amida.springmvc




    
    

    
    

    
    
    
    
    
        
        
    
    


7.新建java文件夹,新建包com.amida.springmvc,新建类DemoController

注意视图页面映射fist

package com.amida.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/demo")
public class DemoController {

    @RequestMapping("/index")
    public String index(){
        return "fist";
    }
}



8.在WEB-INF下新建fist.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




首页


    

This is SpringMVC Demo



9.运行程序

http://localhost:8080/com.amida.springmvc/demo/index

SpringMVC学习-基础环境搭建_第1张图片


10.json支持

 
    org.codehaus.jackson
    jackson-core-asl
    1.9.13

    
        org.codehaus.jackson
        jackson-mapper-asl
        1.9.13
    

    com.fasterxml.jackson.core
    jackson-databind
    2.8.0

11.restful

@Controller
@RequestMapping("/persons")
public class GreetingController {
	 private static final String template = "Hello, %s!";
	    private final AtomicLong counter = new AtomicLong();

	    @RequestMapping(value="/greeting", produces="application/json")
	    @ResponseBody
	    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
	        return new Greeting(counter.incrementAndGet(),
	                            String.format(template, name));
	    }
}



你可能感兴趣的:(SpringMVC学习-基础环境搭建)