SpringMVC的Controller中常用的三种返回值类型

的第三范式返回String类型(逻辑视图)在返回值为String类型的时候可以调用重定向和转发的标签。 用redirect和forward返回,注意:用这两个标签必须写全路径,不适应视图解析器的逻辑视图...
摘要由CSDN通过智能技术生成

SpringMVC 通过注解实现的 Controller 常用的返回值类型主要有三种:返回字符串,返回 ModelAndView,返回 void。

先看一下项目的目录结构:
这里写图片描述

再看一下 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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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
        http://www.springframework.org/schema/context/spring-context.xsd
    	http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
    	http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
	<!-- 开启spring的组件扫描,自动加载bean -->
	<context:component-scan base-package="com.lyu.qjl.interview.controller" />

	<!-- 可以用mvc的注解驱动 --> 
	<mvc:annotation-driven />
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置视图名的默认前缀 -->
		<property name="prefix" value="/"></property>
		<!-- 配置视图名的默认后缀 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
	
</beans>

返回字符串

应用场景1:直接返回视图名称,例如跳转到某个功能主页。

下面的 Controller 是用来处理用户页面的请求,gotoUserEdit 方法是用来处理跳转到用户编辑页面的请求。

package com.lyu.qjl.interview.controller;

import java.util.ArrayList
  • 30
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值