一、向后台传值

1、项目结构

2、jar包

3、spring-config.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:oxm="http://www.springframework.org/schema/oxm"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:task="http://www.springframework.org/schema/task"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  11. http://www.springframework.org/schema/oxm
  12. http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
  13. http://www.springframework.org/schema/context
  14. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  15. http://www.springframework.org/schema/tx
  16. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  17. http://www.springframework.org/schema/aop
  18. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  19. http://www.springframework.org/schema/task
  20. http://www.springframework.org/schema/task/spring-task-3.2.xsd">
  21.  
  22. <!-- 通知spring容器通过注解的方式装配bean -->
  23. <context:annotation-config />
  24. <!-- 通知spring容器采用自动扫描机制查找注解的bean -->
  25. <context:component-scan base-package="com.*" />
  26.  
  27. <task:annotation-driven /> <!-- 定时器开关-->
  28.  
  29. <bean id="agentExcelTask" class="com.timer.TimerController1"/>
  30. <task:scheduled-tasks>
  31. <task:scheduled ref="agentExcelTask" method="printstr" cron="* * 0/1000 * * ?"/>
  32. </task:scheduled-tasks>
  33.  
  34. <!-- 配置返回页面过滤 -->
  35. <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  36. <property name="viewClass"
  37. value="org.springframework.web.servlet.view.JstlView" />
  38. <property name="prefix" value="/" />
  39. <property name="suffix" value=".jsp" />
  40. </bean>
  41. </beans>

4、LoginController.java,下面是spring向后台传值的三种方式。

  1. package com.controller;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4.  
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9.  
  10. import com.demo.User;
  11.  
  12. @Controller
  13. public class LoginController {
  14.  
  15. /**
  16. * 使用HttpServletRequest获取
  17. */
  18. @RequestMapping("/login1")
  19. public String login1(HttpServletRequest request,Model model){
  20. model.addAttribute("name", request.getParameter("name"));
  21. model.addAttribute("password", request.getParameter("password"));
  22. return "success";
  23. }
  24.  
  25. /**
  26. * spring自动将表单参数注入到方法参数,参数值和页面name属性一致时可以省去@RequestParam注解
  27. */
  28. @RequestMapping("/login2")
  29. public String login2(@RequestParam("name") String name, String password,Model model){
  30. model.addAttribute("name", name);
  31. model.addAttribute("password", password);
  32. return "success";
  33. }
  34.  
  35. /**
  36. * 自动注入bean属性
  37. */
  38. @RequestMapping("/login3")
  39. public String login3(User user,Model model){
  40. model.addAttribute("name", user.getName());
  41. model.addAttribute("password", user.getPassword());
  42. return "success";
  43. }
  44.  
  45. }

注:第三种方式自动注入bean属性需要定义实体类,本例中定义User.java

5、User.java

  1. package com.demo;
  2.  
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.Id;
  6.  
  7. @Entity
  8. public class User {
  9.  
  10. @Id
  11. @GeneratedValue
  12. private long id;
  13. private String name;
  14. private String password;
  15. public long getId() {
  16. return id;
  17. }
  18. public void setId(long id) {
  19. this.id = id;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27. public String getPassword() {
  28. return password;
  29. }
  30. public void setPassword(String password) {
  31. this.password = password;
  32. }
  33.  
  34. }

6、index.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7.  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <title>index</title>
  13. </head>
  14.  
  15. <body>
  16. <form action="login2" method="post">
  17. 用户:<input type="text" name="name"><br><br>
  18. 密码:<input type="text" name="password"><br><br>
  19. <input type="submit" value="确定">
  20. </form>
  21.  
  22. <!-- 使用message 标签配置需要显示的国际化文本,
  23. code 对应国际化文件中对应的键的名称 -->
  24. <span style="color: #2D2D2D;">
  25. <spring:message code="main.title"/>
  26. </span>
  27. <br>
  28. <input type="text" value="<spring:message code="main.target"/>">
  29. </body>
  30. </html>

7、success.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>success</title>
  12. </head>
  13.  
  14. <body>
  15. ${name},success. <br>
  16. 用户名:${name},密码:${password}
  17. </body>
  18. </html>

二、向前台传值的两种方式

  1. package com.controller;
  2.  
  3. import java.util.Map;
  4.  
  5. import javax.servlet.http.HttpServletRequest;
  6.  
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.ui.Model;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11.  
  12. import com.demo.User;
  13.  
  14. @Controller
  15. public class LoginController {
  16.  
  17. @RequestMapping("/login2")
  18. public String login2(@RequestParam("name") String name, String password,Model model){
  19. model.addAttribute("name", name);
  20. model.addAttribute("password", password);
  21. return "success";
  22. }
  23.  
  24. return "success";
  25. }
  26.  
  27. @RequestMapping("/login4")
  28. public String login4(User user, Map<String, Object> map){
  29. map.put("name", user.getName());
  30. map.put("password", user.getPassword());
  31. return "success";
  32. }
  33.  
  34. }

三、springmvc重定向

参考spring mvc controller间跳转 重定向 传参 (转)

springmvc之前后台传值的更多相关文章

  1. SpringMVC:前台jsp页面和后台传值

    前台jsp页面和后台传值的几种方式: 不用SpringMVC自带的标签 前台---->后台,通过表单传递数据(): 1.jsp页面代码如下,  modelattribute 有没有都行 < ...

  2. PHP后台传值

    前台数据往后台传值,往往是新手最头痛的,最近在学习thinkPHP的时候,也遇到了这种问题,总结一下,往不足之处请大家指教. 一.前台界面代码,往后台传值有两种方式,一种是get,另一种是post,新 ...

  3. Spring mvc前台后台传值

    前台向后台传值: ①同名参数传递:form表单中提交input,Controller方法入参中,直接以同名参数获取 ②不同名参数传递:from表单提交input,Controller方法入参中需要使用 ...

  4. springmvc用model传值到jsp页面,el表达式引用接收不到传递过来的值

    springmvc用model传值到jsp页面,el表达式引用接收不到传递过来的值 查看下controller层代码可以发现,写的是没有错误的. @RequestMapping("list. ...

  5. ASP.NET MVC 富文本Ueditor编辑 后台传值前端乱码解决方案

    只是将当前内容String当成Html插入,我想是跟数据类型转换差不多 //把内容赋值给ueditor var ue = UE.getEditor('editor');//实例化 ue.ready(f ...

  6. 元素设置disabled属性后便无法向后台传值

    元素设置disabled属性后便无法向后台传值

  7. springMvc 通过url传值,实现访问

    springMvc 通过url传值,实现访问 1.创建web项目,引入相关jar包,并完成相应配置,在上一篇博客已经实现 2.在WEB-INF下创建jsp文件夹,并创建hello.jsp文件 < ...

  8. SpringMVC:后台将List转为Json,传值到页面

    一.JSP页面 <body> <form name="test" action="getAllStudent" method="po ...

  9. 菜鸟学习Spring——SpringMVC注解版前台向后台传值的两种方式

    一.概述. 在很多企业的开法中常常用到SpringMVC+Spring+Hibernate(mybatis)这样的架构,SpringMVC相当于Struts是页面到Contorller直接的交互的框架 ...

随机推荐

  1. ng-controller event data

    $emit只能向parent controller传递event与data $broadcast只能向child controller传递event与data $on用于接收event与data 例子 ...

  2. log4net 记录到数据库和本地文件

    1)配置代码 <?xml version="1.0" encoding="utf-8" ?> <configuration> <c ...

  3. RestSharp使用

    class Program { private readonly static string investRankingForAllUrl = "http://192.168.1.98:90 ...

  4. wifi-mac

    //18:a6:f7:12:0b:8b //18:a6:f7:1e:a9:57 //18:a6:f7:1f:8e:69 //18:a6:f7:12:0b:9c //18:a6:f7:1f:cd:d4 ...

  5. case when then else end

    1.根据数据库表中特定的值进行排序显示 select * from tablename where order by case when columname='' then 1 wnen column ...

  6. Jquery 实现密码框的显示与隐藏【转载自http://blog.csdn.net/fengzhishangsky/article/details/11809069】

    <html> <head>  <script type="text/JavaScript"  src="jQuery-1.5.1.min.j ...

  7. JavaWeb学习笔记——jsp内置对象

  8. centos6.5编译安装git

    1.下载高版本的git,地址:https://github.com/git/git/release,选择git-2.9.3.tar.gz 2.安装依赖包.解压.编译安装 yum install cur ...

  9. ecshop网站搬家缓存无法更新

    问题描述: 1.后台产品列表能改,数据也能看到,前端就是不显示 2.缓存无法删除 3.网上其他方法都试过,还是不行 症状:是因为缓存无法删除,无法更新,只需要能重新更新缓存文件即可.网站搬家丢失tem ...

  10. Python之路【第六篇】:面向对象编程相关

    判断类与对象关系 isinstance(obj, cls)  判断对象obj是否是由cls类创建的 #!/usr/bin/env python #-*- coding:utf-8 -*- class ...