SpringMVC响应

对应响应的 pom.xml文件配置

  • 响应的本质都是传递 数据流
    SpringMVC响应
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>SpringMVC_response</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>


    <dependencies>
        <!--   servlet 规范坐标-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--  jsp坐标-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
            <scope>provided</scope>
        </dependency>
        <!--   spring的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.1.RELEASE</version>
        </dependency>
        <!--        springMvc的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <!--        springweb坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-sdk-java</artifactId>
            <version>3.4.49.ALL</version>
        </dependency>


<!--        对应JSON的坐标-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.0</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>

    <!-- 构建-->
    <build>
        <!--maven插件-->
        <plugins>
            <!--jdk编译插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <!--加入对应的maven版本号码-->
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <!--tomcat插件-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <!--端口控制-->
                    <port>80</port>
                    <!--项目路径控制意味着http://localhost:8080/abc-->
                    <path>/Spring_exploded</path>
                    <!--编码-->
                    <uriEncoding>UTF-8</uriEncoding>
                    <ignorePackaging>true</ignorePackaging>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web.xml 乱码过滤器 以及 DispatcherServlet

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>enconding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--     从类路径下  进行加载文件-->
            <param-value>classpath*:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

不带参数的响应

  • 页面跳转方式 转发和重定向
  • 默认是转发格式
@Controller
public class UserController {


    @RequestMapping("/showpage1")
    public String showPage1(){
        System.out.println("user mvc showpage1 running ");
        return "forward:page.jsp";
    }


    @RequestMapping("/showpage2")
    public String showPage2(){
        System.out.println("user mvc showpage2 running ");
        return "redirect:page.jsp";
    }


//     这种拼接格式  不适用于 forward 以及 redirect
    @RequestMapping("/showpage3")
    public String showPage3(){
        System.out.println("user mvc showpage3 running ");
        return "page";
    }


    @RequestMapping("/showpage4")
    public void showPage4(){
        System.out.println("user mvc controller running ");
    }
}

页面访问快捷设定
展示页面保存位置通常固定 且结构 相似 可以谁当通用的范围跟的路径 检化页面配置格式

<!--   注入访问的 前缀和 访问的 后缀   (类似于 字符串拼接)-->
<!--
        modelAndView.setViewName("forward:page.jsp");
        return modelAndView;
        使用这个的时候  需要将整个注入访问  进行注释
        -->
    <bean   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix"  value="/WEB-INF/page/"/>
        <property name="suffix"  value=".jsp"/>
    </bean>

没有设定返回值的页面跳转
自动进行拼接 和前面的进行跳转

    @RequestMapping("/showpage4")
    public void showPage4(){
        System.out.println("user mvc controller running ");
    }

携带数据页面的跳转设定

  1. 使用HttpServletRequest类型形参进行数据传递
  2. 使用Model类型形参进行数据传递
  3. 使用ModelAndView类型形参进行数据传递 将该对象作为返回值传递给调用者
  4. 使用ModelAndView类型形参进行形参数据传递 将该对象作为返回值转递个调用者

使用ModerlAndView于和 页面访问快捷设定 有冲突 不能一起使用

@Controller
public class BookController {


    @RequestMapping("/showPageAndDate")
    public String showPageAndDate(HttpServletRequest request){
        request.setAttribute("name","罗大林");
        return "page";
    }

    @RequestMapping("/showPageAndDate2")
    public String showPageAndDate2(Model model){
        System.out.println("user mvc showPageAndDate2 running ");
        model.addAttribute("name","罗二林");
        Book book  = new Book();
        book.setBook("SpringMVC入门");
        book.setPrice(666);
        model.addAttribute("book",book);
        return "page";
    }

    @RequestMapping("/showPageAndDate3")
    public ModelAndView showPageAndDate3(ModelAndView modelAndView){
        // ModelAndView M = new ModelAndView();   可以  替换形参中的参数
        Book book  = new Book();
        book.setBook("SpringMVC入门");
        book.setPrice(666);
        modelAndView.addObject("book",book);
        modelAndView.addObject("name","罗三林");

        modelAndView.setViewName("page");
        return modelAndView;
    }


    @RequestMapping("/showPageAndDate4")
    public ModelAndView showPageAndDate4(ModelAndView modelAndView){
        modelAndView.setViewName("forward:page.jsp");
        return modelAndView;
    }

    @RequestMapping("/showPageAndDate5")
    public ModelAndView showPageAndDate5(ModelAndView modelAndView){
        modelAndView.setViewName("redirect:page.jsp");
        return modelAndView;
    }
}

返回值类型是是JSON
需要进行转换

  • 使用response对象完成数据返回
  • 加上@ResponseBody 该注解 可以直接 再页面返回对应的返回值
  • 引入JSON坐标 使用转换器 进行转换
    SpringMVC响应

使用SpringMVC提供的消息类型转换器将对象与集合数据自动转换为JSON数据

开启驱动
SpringMVC响应

对应的实体类

@Controller
public class AccountController {
    @RequestMapping("/showData1")
    public void showData1(HttpServletResponse response) throws IOException {
        response.getWriter().write("message");
    }

    @RequestMapping("/showData2")
    @ResponseBody   // 加上该注解  表示页面展示的就是返回的内容
    public String showData2()  {
       return "message";
    }

    @RequestMapping("/showData3")
    @ResponseBody
    public String showData3() throws JsonProcessingException {

        Book book  = new Book();
        book.setBook("SpringMVC入门");
        book.setPrice(666);

        ObjectMapper om = new ObjectMapper();
       return  om.writeValueAsString(book);

    }

    @RequestMapping("/showData4")
    @ResponseBody
    public Book showData4()  {
        Book book  = new Book();
        book.setBook("SpringMVC入门");
        book.setPrice(666);
        return book;

    }

    @RequestMapping("/showData5")
    @ResponseBody
    public List showData5()  {
        Book book1  = new Book();
        book1.setBook("SpringMVC入门");
        book1.setPrice(666);

        Book book  = new Book();
        book.setBook("SpringMVC入门");
        book.setPrice(666);

        ArrayList list  = new ArrayList();

        list.add(book);
        list.add(book1);

        return list;
    }
}
上一篇:C#随机数


下一篇:No.969 Pancake Sorting