一、配置spring mvc

1、在web.xml文件中添加如下配置:

<servlet><servlet-name>MVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/mvc-config.xml</param-value></init-param>
</servlet>
<servlet-mapping><servlet-name>MVC</servlet-name><url-pattern>*.do</url-pattern>
</servlet-mapping>

2、mvc-config.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:tx="http://www.springframework.org/schema/tx"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/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- 自动扫描的包名 --><context:component-scan base-package="cn.ecgonline.eis.controller." /><!-- 默认的注解映射的支持 --><mvc:annotation-driven /><!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> --><!-- 视图解释类 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 --><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /></bean>
</beans>

二、新建控制器类,获取前台属性值

我用的是annotation方式

1、后台用普通数据类型获取单个属性

控制器类:

package cn.ecgonline.eis.controller.workstation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/*** 测试* @author 陈文龙* @date 2013-9-13 下午12:49:53*/
@Controller
public class WrokstationController
{@RequestMapping("/new_workstation.do")public ModelAndView newWorkStation(@RequestParam Stringusername){System.out.println(user.getUsername);ModelAndView mv = new ModelAndView("Hello");mv.addObject("test", "hello spring mvc!");return mv;}
}

@RequestParam 注释表示 单个属性

jsp页面代码:

<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!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>Insert title here</title>
</head>
<body>
<form action="<%=basePath%>/new_workstation.do" method="post">
<input type="text" name="username"><!--username对应后台控制器中的username-->
<input type="submit" value="测试">
</form>
</body>
</html>

二、后台用model类获取数据

Model类:

package test;
public class User
{private String username;private String password;public String getUsername(){return username;}public void setUsername(String username){this.username = username;}public String getPassword(){return password;}public void setPassword(String password){this.password = password;}
}


控制器类:

package cn.ecgonline.eis.controller.workstation;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/*** 测试* @author 陈文龙* @date 2013-9-13 下午12:49:53*/
@Controller
public class WrokstationController
{@RequestMapping("/new_workstation.do")public ModelAndView newWorkStation(@ModelAttribute User user){System.out.println(user.getUsername);System.out.println(user.getPassword);ModelAndView mv = new ModelAndView("Hello");mv.addObject("test", "hello spring mvc!");return mv;}
}

@ModelAttribute 注解代表用模型来接收值,User对象里面的属性要和jsp页面的属性想对应。



jsp页面代码:

<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!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>Insert title here</title>
</head>
<body>
<form action="<%=basePath%>/new_workstation.do" method="post"><table><tr><th>用户名:</th><td><input type="text" name="username"></td></tr><tr><th>密码</th><td><input type="text" name="password"></td></tr></table><input type="submit" value="测试">
</form>
</body>
</html>


转载于:https://blog.51cto.com/5kids/1296506

spring mvc 前台属性数据的传递和后台属性数据的接收相关推荐

  1. 前台传入数据,在后台servelet中接收的,强硬解决乱码格式

    前台传入数据,在后台servelet中接收的,强硬解决乱码格式 String date = request.getParameter("date");System.out.prin ...

  2. SpringMVC项目前台利用ajaxFileUpload传递图片后台接收

    今天上午一直在看这个问题,就是图片上传到后台之后发现图片无法正确的显示了,很是诧异. 诧异之余,不得不去想为什么会出现这样的情况,那么要看我后台接收参数的方式,在看后台之前,我们先来看看前台是如何上传 ...

  3. 前台js获取url传递参数(后台Request.QueryString接收)

    1.方法 function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^& ...

  4. 前台传String日期格式后台用date接收

    原博客地址:https://www.jianshu.com/p/2e101eaeb8ec 前端form表单是通过日期选择器选择的日期,传的是字符串类型的日期 后面使用实体接收 解决办法就是: 在接收实 ...

  5. Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装

    阅读目录 1. 通过HttpServletRequest获得请求参数和数据 2. 处理方法形参名==请求参数名 3. 如果形参名跟请求参数名不一样怎么办呢?用@RequestParam注解 4. 用实 ...

  6. 关于spring MVC 绑定json字符串与实体类绑定

    1 如果前台传json字符串,后台用@RequestBody 接收 前端 "content-Type":"application/json", 2  前台用fo ...

  7. Spring MVC对象转换说明

    在Spring MVC之前我们需要在Servlet里处理HttpServletRequest参数对象,但这个对象里的属性都是通用类型的对象(如字符串),处理起来很繁琐并且容易出错,而Spring MV ...

  8. java查询图片前台展示_Java +spring MVC 后台图片流在前台页面显示

    用户上传图片后 (头像 or 个人照片等) ,需要在页面上显示,现在提供一种显示方式. 后台采用spring mvc的架构 前台是html Java代码: @ResponseBody @Request ...

  9. Java项目:(小程序)前台+后台相结合水果商城系统(spring+spring mvc+mybatis+layui+微信小程)

    源码获取:博客首页 "资源" 里下载! 一.项目简述 本系统功能包括: 水果商城系统前台展示,分类后买,下单,支付,发货, 地址等等,后台商品上传,用户管理,订单管理,地址管理等等 ...

  10. Java项目:(小程序)前台+后台相结合在线点餐系统(spring+spring mvc+mybatis+layui+微信小程)

    源码获取:博客首页 "资源" 里下载! 一.项目简述 本系统功能包括: 1 .微信小程序扫码点单 2 .微信小程序外卖点单 3 .后台可对微信小程序主页进行自定义(如颜色.布局. ...

最新文章

  1. SQL Server (MSSQLSERVER) 服务因 2148081668 服务性错误而停止。
  2. 2n个整数分为两组,使两组和差的绝对值最小
  3. math标准库函数功能汇总
  4. Java常用API(一)Object
  5. cygwin swoole_swoole入门--------基础概念
  6. tp3.2部署在nginx主页正常,其他页面404问题解决方式
  7. VUE页面实现加载外部HTML方法
  8. Elastic 技术栈之 Filebeat
  9. 哈哈,代码比较工具,好像就这5款比较顺手~~~
  10. 优秀课件笔记之文件系统
  11. netbackup备份mysql_NBU网络备份大全之数据库备份与恢复
  12. java基站定位接口实例
  13. mac 修改hosts不生效问题
  14. SASE 本质探究(1):什么是云化
  15. 看完这个故事终于知道区块链是什么了
  16. 阻容降压电路:每个元器件计算选型!
  17. top.layer.open()是什么东西
  18. 南非SABS EMC CoC简介
  19. Java服务器与客户端传文件,java实现上传文件到服务器和客户端.pdf
  20. qt在表格中如何画线_Qt之表格控件蚂蚁线

热门文章

  1. Layer 引入自定义模块
  2. 调试mysql语句_Mysql sql 语句调试
  3. 全网首发:JDK绘制文字:五、字体上下文产生流程
  4. 梦笔记20211017
  5. sofia-sip-ua >= 1.12.12… configure: error: no usable sofia-sip; please install sofia-sip-ua devel pa
  6. java.library.path设置无效
  7. LINUX C#开发教程
  8. Win10修改注册表,提示无法创建密钥
  9. python保存数组到txt_np.savetxt()——将array保存到txt文件,并保持原格式
  10. c++ primer plus 之函数及函数指针