SpringMVC注解版前台向后台传值的两种方式(IDEA)

一、概述。

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

二、代码演示。

1、首先配置SpringMVC环境。

1.1导入jar。

 

 

 

1.2、xml配置文件。

web.xml

 

<web-app version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>Spring MVC Application</display-name>

    <servlet>
      <servlet-name>mvc-dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>mvc-dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
</web-app>

 

 

 

mvc-dispatcher-servlet.xml

 

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.springapp.mvc"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

 

 



 

2、前台界面代码。

index.jsp

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
 <form action="/method" method="get">
   username:<input type="text" name="username" value=""/>
   <br/>
   password:<input type="text" name="password" value=""/>
   <br/>
   <input type="submit" value="登陆"/>

 </form>
<hr/>
  <a href="/method?username=123&password=456">username</a>
 <br/>
</body>
</html>

 

登陆成功页面

success.jsp

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
<hr/>
<h1>Successful</h1>

username:<%=request.getSession().getAttribute("name")%>
<br/>
password:<%=request.getSession().getAttribute("password")%>
</body>
</html>

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

方式一:

利用@RequestParam这个注解view plai co  

package com.springapp.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class HelloController {
   @RequestMapping(method = RequestMethod.GET)
   public String printWelcome(ModelMap model) {
      model.addAttribute("message", "Hello world!");
      return "hello";
   }
   @RequestMapping(value = "method",method = RequestMethod.GET)
   public String method(@RequestParam("username") String username,
                   @RequestParam("password") String password,
                   HttpServletRequest request)
   {
      System.out.println("come from the page \n" + username + "\n" + password);
      request.getSession().setAttribute("name", username);
      request.getSession().setAttribute("password",password);
      return "success";
   }
}

 

方式二:

方式二中若不加前面的@RequestParam则String后面的变量名一定要与前台输入的变量名称相符合。

否者会报错。也就是说@RequestParam中的参数是从前台表单或者是链接中获取的,赋值给后面的类型对应的变量。

 

package com.springapp.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class HelloController {
   @RequestMapping(method = RequestMethod.GET)
   public String printWelcome(ModelMap model) {
      model.addAttribute("message", "Hello world!");
      return "hello";
   }
   @RequestMapping(value = "method",method = RequestMethod.GET)
   public String method(/*@RequestParam("username")*/ String username,
                   /*@RequestParam("password")*/ String password,
                   HttpServletRequest request)
   {
      System.out.println("come from the page \n" + username + "\n" + password);
      request.getSession().setAttribute("name", username);
      request.getSession().setAttribute("password",password);
      return "success";
   }
}

 

4、界面结果。

第一种传值方式:

 

 

点击登陆后跳转的结果。

 

 

点击超链接后跳转的结果

 

 

第二种传值方式:

 

点击登陆后跳转的结果。

 

点击超链接后跳转的结果。

 

 

三、总结。

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

转载于:https://www.cnblogs.com/zkycode/p/6273880.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值