4、Spring MVC基础配置

1、Spring MVC
  MVC和三层架构的区别:
  MVC:Model+View+Controller 数据模型+视图+控制器
  三层架构:Controller+Service+Dao 展现层+应用层+数据访问层
2、常用注解
  @Controller:表明这个类是Spring MVC中的控制器,声明为Spring的一个Bean,Dispatcher Servlet会自动扫描注解了此注解的类,并将
    Web请求映射到注解了@RequestMapping的方法上
  @RequestMapping:用来映射Web请求(访问路径和方法)到处理类和方法上;注解在方法上的路径会继承注解在类上的路径;支持Servlet
    Request和Response作为参数,也支持对Request和Response的媒体类型进行配置
  @RequestBody:允许request参数在request体内而不是直接链接到地址后面,此注解放置在参数前
  @ResponseBody:支持将返回值放在response体内而不是返回一个页面;在Ajax请求中,可以以此注解返回数据而不是页面;此注解可以
    放置在返回值前或方法上
  @PathVariable:接收路径参数,此注解放置在参数前
  @RestController:组合@Controller和@ResponseBody功能
3、其他
  @RequestMapping如果不指定请求类型,则默认为get
  原生Ajax默认格式:text/plain
  Jquery Ajax默认格式(Map) + Form表单POST提交格式:application/x-www-form-urlencoded
  字符流 Ajax格式(Map):application/json ( 同时使用JSON.stringify(data)将json对象转换为json字符串 )
  文件上传:multipart/form-data
4、示例

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ning.StudentRequestVO;

// @RequestMapping("/stu")指定映射此类的路径为/stu或/stu/xxx
@Controller
@RequestMapping("/stu")
public class StudentController {
    
    //http://localhost:8080/spring4x/stu
    //必须用@ResponseBody指定返回值的形式
    @RequestMapping(produces = "text/plain;charset=UTF-8", method = RequestMethod.GET)
    public @ResponseBody String index(HttpServletRequest request) {
        return "url:" + request.getRequestURI();
    }
    
    //http://localhost:8080/spring4x/stu/paathvar/hello
    //接收路径参数,不指定请求类型则默认为get
    @RequestMapping(value = "/paathvar/{str}", produces = "text/plain;charset=UTF-8")
    public @ResponseBody String stuPathVariable(@PathVariable String str,
            HttpServletRequest request) {
        return "url:" + request.getRequestURI() + "; str=" + str;
    }
    
    //http://localhost:8080/spring4x/stu/param/?str=hello
    //如果参数不是str,比如改为strdd,则str为null,输出为:url:/spring4x/stu/param/; str=null
    @RequestMapping(value = "/param", produces = "text/plain;charset=UTF-8", method = RequestMethod.GET)
    public @ResponseBody String stuRequestParamNormal(String str, HttpServletRequest request) {
        return "url:" + request.getRequestURI() + "; str=" + str;
    }
    
    //http://localhost:8080/spring4x/stu/obj/?id=11&name=helloword
    //没有传递值的参数则为null
    @RequestMapping(value = "/obj", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public @ResponseBody String stuRequestParamObj(StudentRequestVO obj,
            HttpServletRequest request) {
        return "url:" + request.getRequestURI() + "; StudentRequestVO id=" + obj.getId() + ";name="
                + obj.getName() + ";mail=" + obj.getMail();
    }
    
    //http://localhost:8080/spring4x/stu/url01
    //http://localhost:8080/spring4x/stu/url02
    @RequestMapping(value = {"/url01",
            "/url02"}, produces = "text/plain;charset=UTF-8", method = RequestMethod.GET)
    public @ResponseBody String stuMultip(HttpServletRequest request) {
        return "url:" + request.getRequestURI();
    }
    
    //http://localhost:8080/spring4x/stu/getjson
    @RequestMapping(value = "/getjson", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public @ResponseBody StudentRequestVO stuGetJson(HttpServletRequest request) {
        StudentRequestVO stuObj = new StudentRequestVO();
        stuObj.setId(11);
        stuObj.setName("helloword");
        return stuObj;
    }
    
    //http://localhost:8080/spring4x/stu/getxml
    //返回的数据格式为xml
    @RequestMapping(value = "/getxml", produces = "application/xml;charset=UTF-8", method = RequestMethod.GET)
    public @ResponseBody StudentRequestVO stuGetXml(HttpServletRequest request) {
        StudentRequestVO stuObj = new StudentRequestVO();
        stuObj.setId(11);
        stuObj.setName("helloword");
        return stuObj;
    }
}

相关推荐