一,简单无参数地址访问
首先来看下类标记:
-
-
-
- @Controller
- @RequestMapping("/hello")
- public class HelloMvcController {
简单进行类中方法的访问:
-
- @RequestMapping("/mvc")
- public String helloMvc() {
- return "home";
- }
二,使用Servlet api方式获取参数
-
- @RequestMapping("/views3")
- public String viewCourse3(HttpServletRequest request) {
- Integer courseId = Integer.valueOf(request.getParameter("courseId"));
- System.out.println(courseId);
- return "home";
- }
三,通过方法参数方式获取
-
- @RequestMapping(value = "/views", method = RequestMethod.GET)
- public String viewCourse(@RequestParam("courseId") Integer courseId) {
- System.out.println(courseId);
- return "home";
- }
四,restful 风格URL参数获取
-
-
- @RequestMapping(value = "/views/{courseId}", method = RequestMethod.GET)
- public String viewCourse2(@PathVariable("courseId") Integer courseId,
- Map<String, Object> model) {
- System.out.println("restful风格URL示例测试---" + courseId);
-
- return "home";
- }
五,重定向操作
-
- @RequestMapping(value = "/save", method = RequestMethod.POST)
- public String doSave(@ModelAttribute Course course) {
-
- return "redirect:views/" + course.getCourseId();
- }
六,接收上传的文件
-
- @RequestMapping(value="/doUpload",method=RequestMethod.POST)
- public String doUploadFile(@RequestParam("file") MultipartFile file){
-
- if(!file.isEmpty()){
- System.out.println("请在这里写入对文件的操作");
-
- }
-
- return "success";
- }
另外,还需要在spring mvc的配置文件中加入:
-
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name="maxUploadSize" value="209715200"/>
- <property name="defaultEncoding" value="UTF-8"/>
- <property name="resolveLazily" value="true"/>
-
- </bean>