springmvc第八个练习(处理器方法的返回值为String)

处理器方法的返回值
使用 @Controller 注解的处理器的处理器方法,其返回值常用的有四种类型:
第一种: ModelAndView
第二种: String
第三种:无返回值 void
第四种:返回自定义类型对象
根据不同的情况,使用不同的返回值。
2.3.1 返回 ModelAndView
       若处理器方法处理完后,需要跳转到其它资源,且又要在跳转的资源间传递数据,此时 处理器方法返回 ModelAndView 比较好。当然,若要返回 ModelAndView ,则处理器方法中 需要定义 ModelAndView 对象。
      在使用时,若该处理器方法只是进行跳转而不传递数据,或只是传递数据而并不向任何 资源跳转(如对页面的 Ajax 异步响应),此时若返回 ModelAndView ,则将总是有一部分多 余:要么 Model 多余,要么 View 多余。即此时返回 ModelAndView 将不合适。
2.3.2 返回 String
      处理器方法返回的字符串可以指定逻辑视图名,通过视图解析器解析可以将其转换为物理视图地址
返回内部资源逻辑视图名
        若要跳转的资源为内部资源,则视图解析器可以使用 InternalResourceViewResolver 内部 资源视图解析器。此时处理器方法返回的字符串就是要跳转页面的文件名去掉文件扩展名后 的部分。这个字符串与视图解析器中的 prefix suffix 相结合,即可形成要访问的 URI

直接修改处理器类 MyController

        当然,也可以直接返回资源的物理视图名。不过,此时就不需要再在视图解析器中再配 置前辍与后辍了。

项目结构

1.index.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>处理器方法的返回值</h2>

<p>处理器方法返回String,表示视图名称</p>
<form action="returnString-view.do" method="post">
    姓名:<input type="text" name="name"/><br>
    年龄:<input type="text" name="age"/><br>
    <input type="submit" value="提交参数"/>
</form>

<p>处理器方法返回String,表示视图完整路径</p>
<form action="returnString-view2.do" method="post">
    姓名:<input type="text" name="name"/><br>
    年龄:<input type="text" name="age"/><br>
    <input type="submit" value="提交参数"/>
</form>

</body>
</html>

2.show.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<p>show.jsp</p>
<p>/WEB-INF/view/show.jsp从request作用域获取数据</p>
<p>myname数据:${myname}</p>
<p>myage数据:${myage}</p>

</body>
</html>

3.MyController类

package com.it.controller;

import com.it.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

@Controller
public class MyController {


    /**
     *处理器方法返回String--表示逻辑视图名称,需要配置视图解析器
     */
    @RequestMapping(value = "/returnString-view.do")
    public String doReturnView(HttpServletRequest request, String name, Integer age){
        //手动添加数据到request作用域
        request.setAttribute("myname",name);
        request.setAttribute("myage",age);

        //show:逻辑视图名称,项目中配置了视图解析器。框架对视图执行forward转发操作
        return "show";
    }

    /**
     *处理器方法返回String,表示完整视图路径,此时不能配置视图解析器
     */
    @RequestMapping(value = "/returnString-view2.do")
    public String doReturnView2(HttpServletRequest request, String name, Integer age){
        //手动添加数据到request作用域
        request.setAttribute("myname",name);
        request.setAttribute("myage",age);

        //完整视图路径,项目中不能配置视图解析器
        return "/WEB-INF/view/show.jsp";
    }

}

4.springmvc.xml

<?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"
       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">

<!-- 声明组件扫描器-->
<context:component-scan base-package="com.it.controller"/>


<!--声明springmvc框架中的视图解析器,帮助开发人员设置视图文件的路径-->
<!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!--    &lt;!&ndash;   前缀:视图文件的路径&ndash;&gt;-->
<!--    <property name="prefix" value="/WEB-INF/view/"/>-->
<!--    &lt;!&ndash;    后缀:视图文件的扩展名&ndash;&gt;-->
<!--    <property name="suffix" value=".jsp"/>-->
<!--</bean>-->


</beans>

5.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">

<!--
1.声明,注册springmvc核心对象DispatcherServlet
需要在tomcat服务器启动后,创建  DispatcherServlet对象的实例,
因为DispatcherServlet在他的创建过程中,会同时创建springmvc容器对象,
读取springmvc的配置文件时,可以把配置文件都创建好当用户发起请求时就可以直接访问对象


2.servlet初始化会执行init()方法,DispatcherServlet在init()中{
//创建容器,读取配置文件
WebApplicationContext wac=new ClassPathXmlApplicationContext("springmvc.xml");
//把容器对象放入到ServletContext中
getServletContext().setAttribute(key,wac);
}
-->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!--自定义springmvc读取的配置文件的位置-->
    <init-param>
<!--   springmvc的配置文件的位置属性-->
        <param-name>contextConfigLocation</param-name>
<!--   指定自定义文件的位置-->
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>

<!-- 在tomcat启动后,创建Servlet对象
load-on-startup:表示tomcat启动后创建对象的顺序,它的值是整数,数值越小,tomcat创建对象的时间越早(大于等于0的整数)
-->
    <load-on-startup>1</load-on-startup>

</servlet>
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
<!--
使用框架的时候,url-pattern可以使用两种值
1.使用扩展名的方式,语法 *.xxxx,xxxx是自定义的扩展名,常用的方法 *.do,*.action,*.mvc等等。
2.使用斜杠"/"
-->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>


    <!--    注册声明过滤器-->
    <filter>
        <filter-name>characterEncodingFilter</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>
        <!-- 强制请求对象(HttpServletRequest)使用 encoding编码的值  -->
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <!--强制应答对象(HttpServletResponse)使用encoding编码的值 -->
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

项目结果

 

如果想要正常提交,第一个表单需要加上视图解析器,第二个表单不能加上视图解析器(会导致路径错误,发生404错误)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一道光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值