目录

java学习总结三springMVC

java学习总结三:springMVC

一、Springmvc是什么

Springmvc是Spring框架的后续产品,用在基于MVC的表现层开发,类似于struts2框架。

Spring全家桶:SSM+SpringBoot +SpringCloud+Redis+Vue + IDEA+Git

https://i-blog.csdnimg.cn/direct/c9adf5caa3a04770810c5df3f8000ad0.png

https://i-blog.csdnimg.cn/direct/b0fbc8d876c049de975e7a2a0499fcb6.png

Springmvc依赖于Core(IOC),Springmvc需要导入Core包和Springmvc特有的包。

如果使用Spring的DAO、ORM不但要导入这两个包,好如要导入AOP

二、快速入门

@Controller
// 请求映射,怎么样找到这些请求的路径 /student/add
@RequestMapping(value = "/student")
public class StudentController {
   @RequestMapping(value = "/add1")
   public void add1(String name, Integer age, String gender) {
     System.out.println("StudentController.add()");
     System.out.println("name: " + name);
     System.out.println("age: " + age);
     System.out.println("gender: " + gender);
   }
   
   @RequestMapping(value = "/add")
   public ModelAndView add(Student student) {
     System.out.println("StudentController.add()");
     System.out.println(student);
     
     // Model:数据     View:界面
     ModelAndView modelAndView = new ModelAndView();
     // request.setAttribute("student", student);
     modelAndView.addObject("student", student);
     // request.getRequestDispatcher("/student_info.jsp").forward(request,response);
     modelAndView.setViewName("/student_info.jsp");
     return modelAndView;
   }
}

三、ModelAndView和Model

ModelAndView:即放数据,也放转发的页面

Model:只放数据,以方法返回值形式设置转发的页面

@RequestMapping("/add2")
public ModelAndView add2(Student student) {
   System.out.println("StudentController.add2()");
   System.out.println(student);
   // req.setAttritbu("student", student);
   // req.getRequestDispatcher("/student_info.jsp").forword(req, resp);
   ModelAndView modelAndView = new ModelAndView();
   modelAndView.addObject("student", student);
   modelAndView.setViewName("/student_info.jsp");
   return modelAndView;
}

@RequestMapping("/add3")
public String add3(Student student, Model model) {
   System.out.println("StudentController.add3()");
   System.out.println(student);
   // req.setAttritbu("student", student);
   model.addAttribute("student", student);
   // req.getRequestDispatcher("/student_info.jsp").forword(req, resp);
   return "/student_info.jsp";
}

四、RequestMethod

可以限定某个业务控制方法,只允许GET或者POST方式请求访问:

五、写入request、response等传统web参数

@RequestMapping(value = "/add", method = RequestMethod.POST)
public void add(HttpServletRequest request, HttpServletResponse response, HttpSession session)
     throws ServletException, IOException {
   System.out.println("StudentController.add()");
   String name = request.getParameter("name");
   String age = request.getParameter("age");
   String gender = request.getParameter("gender");
   Student student = new Student(null, name, Integer.parseInt(age), gender);
   request.setAttribute("student", student);
   request.getRequestDispatcher("/student_info.jsp").forward(request, response);
}

七、转发&重定向

转发和重定向:

//http://localhost:8080/student/deleteById?id=23
@RequestMapping("/deleteById")
public String deleteById(Integer id) {
    System.out.println("StudentController.deleteById");
    System.out.println("id: " + id);
    return "redirect:/student/selectAll";
}

@RequestMapping("/selectAll")
public String selectAll() {
    System.out.println("StudentController.selectAll");
    return "/student_list.jsp";
}

八、乱码问题

在传统JSP+Servlet中我们自己写Filter来处理乱码问题,使用SpringMVC他帮我们写了一个处理乱码问题的Filter,我们只需要在web.xml中配置这个Filter就可以了。

在web.xml中添加

<!-- 解决POST乱码问题 -->
 <filter>
     <filter-name>characterEncoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
 </filter>
 <filter-mapping>
     <filter-name>characterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>

九、JSON数据封装

1、加入Jackson相关jar包

2、在返回方法的时候加上@ResponseBody

@RequestMapping("/selectById")
@ResponseBody
public Student selectById(Integer id){
    Student student = new Student(1, "张三1", 23, "男");
    return student;
}

@RequestMapping("/select")
@ResponseBody
public List<Student> select(){    
    Student student1 = new Student(1, "张三1", 23, "男");
    Student student2 = new Student(2, "张三2", 23, "男");
    Student student3 = new Student(3, "张三3", 23, "男");
    List<Student> list = new ArrayList<>();
    list.add(student1);
    list.add(student2);
    list.add(student3);
    return list;
}

十、@RequestParam注解

我们一般使用的参数绑定都有遵循的规则:方法参数名要与传递过来的name属性名相同。

在默认的情况下,只有名字相同,SpringMVC才会帮我们进行参数绑定…

如果我们使用@RequestParam注解的话,我们就可以使方法参数名与传递过来的name属性名不同…

该注解有三个变量:

1、value【指定name属性的名称是什么】

/student/selectByPage?page=3&pageSize=34

public void selectByPage(@RequestParam(value = “page”, defaultValue = “1”) Integer pageNo,Integer pageSize)

2、required 代表这个参数必须要传递过来,原来不传默认是null,加了这个required 后不传就会报错

3、defaultvalue设置默认值

使用了defaultValue,required只能为false,前端不传参数时会将参数置为defaultValue

@RequestMapping("/selectByPage")
public String selectByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNo,
                           @RequestParam(defaultValue = "5") Integer pageSize,
                           @RequestParam(required = true, defaultValue = "4") Integer totalPage) {
    System.out.println("pageNo: " + pageNo);
    System.out.println("pageSize: " + pageSize);
    System.out.println("defaultValue: " + totalPage);

    return "/student_list.jsp";
}

十一、视图解析器

<!-- 视图解析器
1、如果Controller中书写的是视图的逻辑名,这个视图解析器必须要配置。
前缀+视图逻辑名+后缀=真实路径
/ + student_add + .jsp
2、如果视图解析器书写的是视图的真实路径,那么这个视图解析器可以不配置
 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <!-- 路径前缀 -->
   <property name="prefix" value="/"/>
   <!-- 路径后缀 -->
   <property name="suffix" value=".jsp"/>
</bean>