框架---SpringMVC

Springmvc是什么

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

一般的项目:SSM+SpringBoot+IDEA+Git+SpringCloud+Redis

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

web.xml

是Web的核心配置文件。要求:看得懂
在这里插入图片描述

springmvc.xml

SpringMVC的核心配置文件

示例

非常简洁

@RequestMapping("/student")
//既支持POST也支持GET

添加

可以限定某个业务控制方法,只允许GET或者POST方式请求访问。
否则会报405,不支持请求

@Controller
@RequestMapping("/student")
public class StudentController {
//@RequestMapping("/insert")省略形式,只有这一个的时候
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
//当有其他要求的时候不能省略,method = RequestMethod.POST规定必须使用POST方法(默认GET)
    public String insert(Student student, Model model) {
        System.out.println("StudentController.insert");
        System.out.println(student);

        model.addAttribute("student",student);
        return "/jsp/student_info.jsp";
    }
}

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

@RequestMapping("/insert2")
public ModelAndView insert2(Student student) {
   System.out.println("StudentController.insert2()");
   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;
}

在业务逻辑中收集数组

请添加图片描述

<body>
    <form action="${pageContext.request.contextPath}/student/delete.action" method="post">
        <table border="1">
            <tr>
                <th>编号</th>
                <th>姓名</th>
            </tr>
            <tr>
                <td><input type="checkbox" name="ids" value="1"/></td>
                <td>张三</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="ids" value="2"/></td>
                <td>李四</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="ids" value="3"/></td>
                <td>王五</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="ids" value="4"/></td>
                <td>赵六</td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="提交"/></td>
            </tr>
        </table>
    </form>
</body>
@RequestMapping("delete")
    public void delete(Integer[] ids, HttpServletRequest request){
    //HttpServletRequest request依然可以使用。
        System.out.println("StudentController.delete");
        String[] idsArr = request.getParameterValues("ids");
        //但是request.getParameterValues返回的只能是String[]。
        System.out.println("idsArr : " + Arrays.toString(idsArr));
        //request.getParameterValues能直接得,直接打印即可。
        System.out.println("ids : " + Arrays.toString(ids));
    }

JSON数据封装:

1、加入Jackson相关jar包
2、在返回方法的时候加上@ResponseBody

@RequestMapping("/selectById")
@ResponseBody
public Student selectById(Integer id){}

@RequestMapping("/select")
@ResponseBody
public List<Student> select({}

GET和POST

GET:URL:http://localhost:8080/SpringMVC/student/deleteAll.action?ids=1&ids=2&ids=4
POST:
请添加图片描述

// /SpringMVC/student/deleteById.action?id=23
@RequestMapping("/deleteById")
    public String deleteById(Integer id) {
        System.out.println("StudentController.deleteById");
        System.out.println("id: " + id);

        // response.sendRedirect("/SpringMVC/student/selectAll.action");
        return "redirect:/student/selectAll.action";
    }
// 参数作为路径的一部分 (了解)
    // /SpringMVC/student/deleteById1/12.action
@RequestMapping("/deleteById1/{id}")
    public String deleteById1(@PathVariable("id") Integer id) {
        System.out.println("StudentController.deleteById1");
        System.out.println("id: " + id);

        // response.sendRedirect("/SpringMVC/student/selectAll.action");
        return "redirect:/student/selectAll.action";
    }

乱码

在传统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>

转发和重定向

// /SpringMVC/student/deleteById.action?id=3
@RequestMapping("/deleteById")
public String deleteById(Integer id) {
   System.out.println("StudentController.deleteById()");
   System.out.println("id: " + id);
   
   // response.sendRedirect("/JavaSpringMVC/student/selectAll.action");
   return "redirect:/student/selectAll.action";//也很简洁
}

@RequestParam注解

@RequestMapping("/deleteById1/{id}")
    public String deleteById1(@RequestParam(value = "id") Integer id) {//重命名
        System.out.println("StudentController.deleteById1");
        System.out.println("id: " + id);

        // response.sendRedirect("/SpringMVC/student/selectAll.action");
        return "redirect:/student/selectAll.action";
    }
@RequestMapping("/deleteById1/{id}")
    public String deleteById1(@RequestParam(required = true) Integer id) {//这个参数必须有
        System.out.println("StudentController.deleteById1");
        System.out.println("id: " + id);

        // response.sendRedirect("/SpringMVC/student/selectAll.action");
        return "redirect:/student/selectAll.action";
    }
@RequestMapping("/deleteById1/{id}")
    public String deleteById1(@RequestParam(defaultValue = "10") Integer id) {//设置默认值
        System.out.println("StudentController.deleteById1");
        System.out.println("id: " + id);

        // response.sendRedirect("/SpringMVC/student/selectAll.action");
        return "redirect:/student/selectAll.action";
    }

视图解析器

< !-- 视图解析器
1、如果Controller中书写的是视图的逻辑名,这个视图解析器必须要配置。
前缀+视图逻辑名+后缀=真实路径
/jsp/ + student_insert + .jsp
2、如果视图解析器书写的是视图的真实路径,那么这个视图解析器可以不配置
– >

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <!-- 路径前缀 -->
   <property name="prefix" value="/jsp/"/>
   <!-- 路径后缀 -->
   <property name="suffix" value=".jsp"></property>
</bean>

拦截器

Spring Web MVC 的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理。常见应用场景
1、日志记录:记录请求信息的日志,以便进行信息监控、信息统计等。
2、权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直接返回到登录页面;
3、性能监控:有时候系统在某段时间莫名其妙的慢,可以通过拦截器在进入处理器之前记录开始时间,在处理完后记录结束时间,从而得到该请求的处理时间
…………本质也是AOP(面向切面编程),也就是说符合横切关注点的所有功能都可以放入拦截器实现。
请添加图片描述

springmvc.xml:
<!-- 配置拦截器 -->
<mvc:interceptors>
   <mvc:interceptor>
     <mvc:mapping path="/**"/>
     <bean class="com.situ.mvc.interceptor.MyInterceptor1"/>
   </mvc:interceptor>
   <mvc:interceptor>
     <mvc:mapping path="/**"/>
     <bean class="com.situ.mvc.interceptor.MyInterceptor2"/>
   </mvc:interceptor>
</mvc:interceptors>

MyInterceptor1.preHandle()
StudentController.selectAll()
MyInterceptor1.postHandle()
MyInterceptor1.afterCompletion()

MyInterceptor1.preHandle()
MyInterceptor2.preHandle()
StudentController.selectAll()
MyInterceptor2.postHandle()
MyInterceptor1.postHandle()
MyInterceptor1.afterCompletion()
MyInterceptor1.afterCompletion()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值