直接上核心代码

package controller;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;import pojo.Student;//@Controller标注是一个类是Web控制器,
//url可以对这个类进行访问
@Controller
public class TestController {//因为web.xml里面定义了“映射路径.do”//所以,URL路径为t5.do@RequestMapping("t5")public void test5(HttpServletRequest request) {//传递参数到页面,使用request//测试路径:http://localhost:8080/testSpringMVC0/t5.do?name=zhangsan&age=10//返回值类型是void,因为在SpringMVC中配置了前缀和后缀,//所以,默认跳转到 “前缀+映射路径+后缀”页面,即/WEB-INF/t5.jsp页面String name = request.getParameter("name");request.setAttribute("name", name);}/*@RequestMapping("t5")public void test5(Student stu,HttpServletRequest request) {//这种写法也是可以的request.setAttribute("stu", stu);}*/@RequestMapping("t6")public void test6(Student stu,Model model) {//传递参数到页面,使用Model//测试路径:http://localhost:8080/testSpringMVC0/t6.do?name=zhangsan&age=10//返回值类型是void,因为在SpringMVC中配置了前缀和后缀,//所以,默认跳转到 “前缀+映射路径+后缀”页面,即/WEB-INF/t6.jsp页面model.addAttribute("stu", stu);}/*@RequestMapping("t7")public void test7(Student stu) {//void传递参数到页面,不能使用ModelAndView//测试路径:http://localhost:8080/testSpringMVC0/t7.do?name=zhangsan&age=10//返回值类型是void,因为在SpringMVC中配置了前缀和后缀,//所以,默认跳转到 “前缀+映射路径+后缀”页面,即/WEB-INF/t7.jsp页面ModelAndView mv =new ModelAndView();mv.setViewName("t7");mv.addObject("stu", stu);}*/
}

另外需要注意

导入依赖

1.导入相关jar包(非Maven)

2.导入相关依赖的坐标(Maven)

SpringMVC配置文件配置前后缀

<?xml version="1.0" encoding="UTF-8"?>
<!-- 下面的xmlns是定义xml的解释性文件,确定xml的书写规则 -->
<beans default-lazy-init="true"xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"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-3.0.xsd  http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- 使用注解的包,包括子集 --><context:component-scan base-package="controller" /><!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --><mvc:annotation-driven /><!-- 视图解析器 --><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><!-- 当返回值类型为void时,默认跳转到 “前缀+映射路径+后缀”页面 --><!-- 跳转页面的前缀 --><property name="prefix" value="/WEB-INF/" /><!-- 跳转页面的后缀 --><property name="suffix" value=".jsp"></property></bean><!-- 允许通过的静态资源路径 --><mvc:resources mapping="/js/**" location="/js/" /><mvc:resources mapping="/css/**" location="/css/" /><mvc:resources mapping="/fonts/**" location="/fonts/" />
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"><!-- 配置Servlet --><!-- 所有*.do的路径都会通过DispatcherServlet处理--><!-- 将*.do变为*,会拦截所有静态文件--><servlet><servlet-name>SpringMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><!-- 设置配置文件的名字 --><param-name>contextConfigLocation</param-name><!-- classpath是指在tomcat——》webapps——》项目工程名——》WEB-INF——》classes下的文件 --><!-- springmvc.xml可以保存在src下面,编译时,src下会自动编译到classes文件夹下 --><param-value>classpath:springmvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>SpringMVC</servlet-name><!-- 定义映射路径,所有的映射路径都会默认加上.do --><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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>characterEncodingFilter</filter-name><!-- 所有路径 --><url-pattern>/*</url-pattern></filter-mapping><!-- 定义默认欢迎页面 --><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list>
</web-app>

SpringMVC(2)中返回值void的跳转页面传递参数相关推荐

  1. 【vue-router①】router-link跳转页面传递参数 - 进击的前端之路(偶尔爬坑java小路) - SegmentFault 思否

    在vue项目中,往往会遇到这样的情况,就是要实现在一个循环列表中,点击其中一条跳转到下个页面,然后将这一条的相关数据带到下个页面中显示,这是个循环列表,无论点哪一条都是跳到相同的页面,只是填的数据不一 ...

  2. PHP允许输入负数,php exec在linux中返回值不能为负数

    php exec在linux中返回值不能为负数有需要的朋友可参考一下. 我们先来了解一下关于exec函数的使用方法 exec() 原型:  代码如下 复制代码 string exec (string ...

  3. php构造函数里抛出异常_php-在类的构造函数中返回值

    php-在类的构造函数中返回值 到目前为止,我有一个带有构造函数的29447791671682017201728类 public function __construct ($identifier = ...

  4. 解决 sql 语句正确,日志打印正确,但是在mapper测试中返回值为 NULL

    解决 sql 语句正确,日志打印正确,但是在mapper测试中返回值为 NULL

  5. jq中ajax的res是什么意思,关于jquery ajax中返回值的问题

    已解决问题 收藏 关于jquery ajax中返回值的问题 80 [ 标签:jquery,&nbspajax ] conn.open(); 我的星星页面 2009-10-25 11:52 JQ ...

  6. uniapp在onLaunch中使用redirectTo或reLaunch跳转页面后点击事件失效

    问题描述: 使用uniapp编译成小程序时,在生命周期onLaunch中运用redirectTo或reLaunch跳转页面后点击事件失效,但是如果你重定向的页面中有使用navigator组件跳转后再返 ...

  7. [Xcode 实际操作]九、实用进阶-(24)使用Segue(页面的跳转连接)进行页面跳转并传递参数...

    目录:[Swift]Xcode实际操作 本文将演示使用Segue(页面的跳转连接)进行页面跳转并传递参数. 参照结合:[Xcode10 实际操作]九.实用进阶-(23)多个Storyboard故事板中 ...

  8. vue跳转页面携带参数并且立即执行方法

    1.首先定义跳转函数 这个是链接跳转 <a href="javascript:void(0)" onclick="openPage()">位移变化趋 ...

  9. php页面跳转参数传递参数,php页面跳转怎样传递参数

    [摘要] PHP即"超文本预处理器",是一种通用开源脚本语言.PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言.PHP独特的语法混合了C.Java.Perl以及 ...

最新文章

  1. 张一鸣宣布卸任字节CEO!网友:完不成OKR被优化了!
  2. java流与文件——文本输入输出
  3. LeetCode 278. 第一个错误的版本(二分查找)
  4. CSS 让数字滑动显示
  5. 智能优化算法:乌燕鸥优化算法-附代码
  6. QT打包后音频不响应的问题
  7. 松下PLC连接海创-IIoT平台案例
  8. xz1刷Android10,索尼xz1国行版安卓9.0固件
  9. 考研二战日记——第二天 高数第一章第二节:数列的极限
  10. html a 标签 邮件超链接 发送邮件
  11. Java多线程---Phaser
  12. 微软SQL Server BI认证专家QQ群36882826
  13. SpringBoot服务监控之Actuate
  14. 系统集成十大项目管理(1)
  15. 页面收录和关键词选取
  16. QT里面Q_PROPERTY的使用
  17. CSDN Markdown模板
  18. 基于R语言的seasonal包使用手册_05.final\original\trend\irregular\residuals
  19. 解决树莓派4B连接wifi连接不上的问题
  20. 尚硅谷李玉婷老师MySQL课程--DDL语言

热门文章

  1. linux timewait 时间,linux下释放TIME_WAIT方法
  2. 虚拟内存以及进程的虚拟内存分布(第六章)
  3. 2023 goto 解密脚本源码
  4. docker swarm搭建Redis哨兵(Sentinel)模式
  5. Zabbix控制系统添加被监控的主机(步骤详细)
  6. 机器视觉硬件篇--线激光3d相机介绍及编程
  7. spring boot的简介
  8. 手撕前端面试题(Javascript~事件委托、数组去重、合法的URL、快速排序、js中哪些操作会造成内存泄漏......
  9. 系统工程(SE)学习笔记(三)——需求工程(下)
  10. 计算机作品三等奖——计划清单APP