2.基于注解使用springMVC

 

ps:400的问题出错在请求 500的问题出错在代码 

web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--配置dispatchServlet:这个是mvc的核心:请求分发器,前端控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--绑定mvc的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
            <!--启动级别 服务器一启动就启动-->
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自动扫描包,让指定包下的注解生效,交由ioc容器统一管理-->
    <context:component-scan base-package="com.wu.controller"/>
<!--让springmvc不处理静态资源 css等-->
    <mvc:default-servlet-handler/>
<!--支持mvc注解驱动 在spring中一般采用@RequestMapping 注解来完成映射关系
想使用这个注解必须注册DefaultAnnotationHandlerMapping和一个AnnotationMethodHandlerAdapt实例
这两个实例分别在类级别和方法级别处理 而annotation-driver配置帮助我们自动完成上述两个实例的注入
-->
    <mvc:annotation-driven/>
<!--处理器映射器-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--处理器解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

controller类

package com.wu.controller;

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

/**
 * @author wuyimin
 * @create 2021-06-10-12:11
 * @description
 */
@Controller
@RequestMapping("/hello")//第一级别的路径
public class HelloController {
    @RequestMapping("/h1")//真实访问地址 项目名/HelloController/hello/h1
    public String hello(Model model){
        //封装数据
        model.addAttribute("msg" ,"Hello,Annotation");
        return "hello";//会被视图解析器处理 //web-inf/jsp/hello.jsp
    }
    @RequestMapping("/h2")//真实访问地址 项目名/HelloController/hello/h1
    public String hello2(Model model){//model用于传递数据 modelandView传递数据与视图
        //封装数据
        model.addAttribute("msg" ,"Hello2,Annotation");
        return "hello";//会被视图解析器处理 //web-inf/jsp/hello.jsp
    }

}

jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

 restful风格

@Controller
public class RestFulController {
    //http://localhost:8080/springMVC_02/add?a=1&b=1 原来的方式 加了注解以后此方式就不能用了
    //restful http://localhost:8080/springMVC_02/add/a/b 使用注解@PathVariable
    //@RequestMapping(value="/add/{a}/{b}",method = RequestMethod.GET)方式一
    @GetMapping("/add/{a}/{b}")//方式二
    public String test01(@PathVariable int a,@PathVariable int b, Model model){
        int res=a+b;
        model.addAttribute("msg","结果为"+res);
        return "hello";
    }
}

转发与重定向

@Controller
public class ModelTest01 {
    @RequestMapping("/m1/t1")
    public String test(Model model){
        model.addAttribute("msg","modelTest");
        return "hello";//默认的情况就是转发
    }
    @RequestMapping("/m1/t2")
    public String test02(Model model){

        return "redirect:/index.jsp";//默认的情况就是转发
    }

}

 三个参数的区别

 

posted @ 2021-06-10 12:32  一拳超人的逆袭  阅读(54)  评论(0编辑  收藏  举报