springMVC参数传递(三)
2019独角兽企业重金招聘Python工程师标准>>>
第一步:web.xml文件是一个工程必不可少的配置文件。
<!-- lang: xml -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springmvc-chapter2</display-name>
<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><!-- 配置Servlet的分发以及对springMVC文件的初始路径的引用 -->
<servlet><servlet-name>chapter1</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath*:config/chapter1-servlet.xml</param-value></init-param><!-- 标识启动容器时初始化该Servlet --><load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping><servlet-name>chapter1</servlet-name><url-pattern>/</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>
</filter>
<filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
第二步:springMVC的配置文件,chapter1-servlet.xml
<!-- lang: 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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- 开启注解,因为每次启动都会调用下面两个类,所以springMVC将其集成到一个配置里面,配置了这个,下面两个启用注解的类就可以不配置了 --><mvc:annotation-driven /><!-- 此配置的作用是用来扫描所有需要注解的类,只有被扫描的类才能使用注解 --><context:component-scan base-package="cn.javass.chapter2.web.controller"/><!-- 启用注解,这两个类是使用注解时必须所配置的类,第一个作用是通过url找到类,第二个作用是通过类找到方法--><!-- <beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> --><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"></property><property name="suffix" value=".jsp"></property></bean>
</beans>
第三步:定义一个表单提交的jsp页面,hello.jsp和一个处理后的跳转jsp页面,mutil.jsp
<!-- lang: html -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<script type="text/javascript">
function add(){//获取表单var form = document.getElementById("addForm");//设置请求方式为post,如果是get的话就要对发送过去的参数先进行编码然后再解码,此处不作演示,否则会乱码form.method="post";//指定访问路径form.action = "add";//提交表单form.submit();
}
</script><body>
<form id="addForm">姓名:<input type="text" name="userName"/>年龄:<input type="text" name="age"/><br/><input type="button" value="提交" onclick="add()"/>
</form></body>
</html>
mutil.jsp
<!-- lang: html -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<!-- 此处就通过bean来获取其参数 -->
姓名:${user.userName }<br/>
年龄:${user.age }
</body>
</html>
第四步:建立一个Controller,MutilController.java
<!-- lang: java -->
package cn.javass.chapter2.web.controller;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;@Controller
@RequestMapping("/test")
public class MutilController extends MultiActionController {/*** form表单提交后进入此方法进行处理* @param request* @param response* @param userName form表单中提交的元素userName* @param age form表单中提交的元素age* @return mutil.jsp页面*/
@RequestMapping("/add")
public String add(User user){//通过实体类来封装jsp页面System.out.println("姓名:======"+user.getUserName());System.out.println("年龄:======"+user.getAge());return "/mutil";
}/*** 首先页面请求进入hello.jsp页面* @param request* @param response* @return*/
@RequestMapping("/hello")
public String test(HttpServletRequest request,HttpServletResponse response){return "/hello";}
}
启动tomcat,访问http://localhost:8080/springmvc-chapter2/test/hello,表单提交页面hello.jsp
点击提交,跳转到mutil.jsp页面,控制台打印信息和jsp页面如下图
至此,springMVC通过bean进行参数传递就完成了!
转载于:https://my.oschina.net/hugohxb/blog/182088
springMVC参数传递(三)相关推荐
- 使用SpringMVC参数传递时,解决get请求时中文乱码的问题
问题描述: 使用SpringMVC参数传递时, 遇到get请求中文信息时,页面应答会显示中文乱码. 解决办法: 一, 我们需要把request.getParameter("参数名" ...
- 用SpringMVC参数传递时,解决get请求时中文乱码的问题
问题描述: 使用SpringMVC参数传递时, 遇到get请求中文信息时,页面应答会显示中文乱码 解决办法: 方法一 把request.getParameter("参数名")获取到 ...
- 函数参数传递三种方式(传值方式,地址传递,引用传递)
函数参数传递三种方式(传值方式,地址传递,引用传递) 形参与实参 形参即形式上的参数,对实参的一种抽象类型描述只是声明一个函数能够接受什么类型的实参,而不确定接受的实参的具体内容是什么 实参即传递给函 ...
- SpringMVC基础三
一.解决中文乱码问题 1.请求中文乱码 1.1get请求乱码问题 在toncat8.0以前需要在tomcat/conf/server.xml新增URIEncoding="UTF-8" ...
- SpringMvc参数传递
SpringMvc两种映射规则 1.spring mvc 默认参数映射规则:名字一致 自动映射 2.spring mvc自定义映射规则:可以通过注解来实现=> @RequestParam 传 ...
- SpringMVC(三)-- springmvc的系统学习之数据的处理,乱码及restful
资源:尚学堂 邹波 springmvc框架视频 一.提交数据的处理 1.提交的域名称和处理方法的参数一致 (1)提交的数据:http://localhost:8080/data/hello.do?na ...
- SpringMVC(三)Restful风格及实例、参数的转换
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.Restful风格 1.Restful风格的介绍 Restful 一种软件架构风格.设计风格,而不是 ...
- (转)SpringMVC学习(三)——SpringMVC的配置文件
http://blog.csdn.net/yerenyuan_pku/article/details/72231527 读者阅读过SpringMVC学习(一)--SpringMVC介绍与入门这篇文章后 ...
- 01基于配置文件方式的SpringMVC,三种HandlerMapping,三种控制器
1 添加Spring MVC所需的jar包. 2 创建一个以下项目结构的springmvc项目 3 web.xml的配置如下: <?xmlversion="1.0"en ...
最新文章
- 关于OpenGL ES 3D 光晕如何产生的自我理解
- [Poj 1459] 网络流(一) {基本概念与算法}
- 【转】关于CLR内存管理一些深层次的讨论[下篇]
- URLDecoder: Illegal hex characters in escape (%) pattern ...
- 刚到的新公司 前任经理匆匆见了一面 飞鸽传书下载
- 0830通用问题解决
- java 时间类型添加_java date类型 怎么 插入 时间 到 数据库
- 80多个Ajax解决方案
- 数据库练习(1)——建立数据库
- 注册美国iTunes账号步骤(跳过绑定银行卡)
- kindle看pdf乱码_kindle阅读pdf中文乱码解决
- 冬夜读书示子聿 鉴赏
- 一种点到特定高维平面的距离的优化算法
- syskey (win7启动密码)加密和破解方法
- uc收藏导入html,win10系统下UC浏览器如何导入其他浏览器的收藏夹
- 牛逼了,利用Python实现“天眼系统”,只要照片就能了解个人信息
- 在Wireshark中按进程过滤
- python帕多瓦数列前n项和的_几种求数列前n项和的方法
- sapvl10a增强_教你搞定SAP屏幕增强
- 安装PhotoShop报错 无法写入注册表值请检查权限(错误代码160)