详解SpringMVC注解版前台向后台传值的两种方式

 更新时间:2017年04月12日 14:36:15   作者:gwblue  
本篇文章主要介绍了详解SpringMVC注解版前台向后台传值的两种方式,具有一定的参考价值,有兴趣的可以了解一下。

一、概述。

在很多企业的开法中常常用到SpringMVC+Spring+Hibernate(mybatis)这样的架构,SpringMVC相当于Struts是页面到Contorller直接的交互的框架也是界面把信息传输到Contorller层的一种架构,通过这个架构可以让我们把页面和Contorller层解耦,使得开发人员的分工更加明确。

二、代码演示。

1、首先配置SpringMVC环境。

1.1导入jar。

值得注意的是红色标记的commons-logging这个jar包一定得引入进去不然会报错。

1.2、xml配置文件。

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> 
    <servlet-name>springMVC</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>springMVC</servlet-name> 
    <url-pattern>*.spring</url-pattern> 
  </servlet-mapping> 
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app> 

springMVC-servlet.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:context="http://www.springframework.org/schema/context" 
  xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-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="com.gaowei.controller" /> 
</beans> 

2、前台界面代码。

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
  pageEncoding="UTF-8"%> 
<!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="login.spring" method="post"> 
  username:<input type="text" name="username"> 
  <br/> 
  password:<input type="text" name="password"> 
  <br/> 
  <input type="submit" value="登录"> 
</form> 
</body> 
</html> 

No.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
  pageEncoding="UTF-8"%> 
<!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> 
No! 
</body> 
</html> 

Ok.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
  pageEncoding="UTF-8"%> 
<!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> 
 OK! welcome:${username} 
</body> 
</html> 

3、Contorller层接收前台的两种方式。

方式一:

利用@RequestParam这个注解

package com.gaowei.controller; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 
 
@Controller 
public class Login { 
 
  //方式一 
  @RequestMapping("/login") 
  public String login(@RequestParam("username") String username, 
            @RequestParam("password") String password,Model model){ 
    if (username.equals(password))  
    { 
      model.addAttribute("username", username); 
      return "ok.jsp"; 
    } else { 
      return "no.jsp"; 
    } 
  } 
} 

方式二:

package com.gaowei.controller; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 
 
@Controller 
public class Login { 
@RequestMapping("/login") 
  public String login(String username,String password,Model model){ 
    if (username.equals(password))  
    { 
      model.addAttribute("username", username); 
      return "ok.jsp"; 
    } else { 
      return "no.jsp"; 
    } 
  } 
 
} 

4、界面结果。

第一种传值方式:

第二种传值方式:

三、总结。

这里体现出了SpringMVC传值方式的多样性满足了开发人员的不同需求。第一种用来表单的提交。第二种用来界面间相互传值,也为了方便开发人员利用AJAX。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 浅析java class 文件

    浅析java class 文件

    以下是对java中的class文件进行了详细的介绍,需要的朋友可以过来参考下
    2013-08-08
  • Spring Security 登录时添加图形验证码实现实例

    Spring Security 登录时添加图形验证码实现实例

    这篇文章主要为大家介绍了Spring Security 登录时添加图形验证码实现实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Java 项目中使用递归的小结

    Java 项目中使用递归的小结

    在 Java 中,递归是指在方法的定义中调用自身的过程,递归是基于方法调用栈的原理实现的:当一个方法被调用时,会在调用栈中创建一个对应的栈帧,包含方法的参数、局部变量和返回地址等信息,这篇文章主要介绍了Java 项目中对使用递归的理解分享,需要的朋友可以参考下
    2024-07-07
  • springboot+vue 若依项目在windows2008R2企业版部署流程分析

    springboot+vue 若依项目在windows2008R2企业版部署流程分析

    这篇文章主要介绍了springboot+vue 若依项目在windows2008R2企业版部署流程,本次使用jar包启动后端,故而准备打包后的jar文件,需要的朋友可以参考下
    2022-12-12
  • 关于Java集合框架的总结

    关于Java集合框架的总结

    下面小编就为大家带来一篇关于Java集合框架的总结。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • springcloud-feign调用报错问题

    springcloud-feign调用报错问题

    这篇文章主要介绍了springcloud-feign调用报错问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • Spring Security 强制退出指定用户的方法

    Spring Security 强制退出指定用户的方法

    本篇文章主要介绍了Spring Security 强制退出指定用户的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • 深入理解Mybatis二级缓存

    深入理解Mybatis二级缓存

    与一级缓存相比,二级缓存范围更大了一些,可以被多个SqlSession所共用。下面通过本文带领大家一起学习mybatis二级缓存知识,一起看看吧
    2016-12-12
  • Mybatis-Plus 自动填充失效问题解决

    Mybatis-Plus 自动填充失效问题解决

    在使用MyBatis-Plus时,自动填充功能可能会失效,这通常与版本和配置有关,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-10-10
  • springboot项目打包镜像方式以及区分环境打包的方法

    springboot项目打包镜像方式以及区分环境打包的方法

    本文主要介绍了springboot项目打包镜像方式以及区分环境打包的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-03-03

最新评论