zoukankan      html  css  js  c++  java
  • 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 
    " + username + "
    " + 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 
    " + username + "
    " + password);
          request.getSession().setAttribute("name", username);
          request.getSession().setAttribute("password",password);
          return "success";
       }
    }

    4、界面结果。

    第一种传值方式:

     

    点击登陆后跳转的结果。

     

    点击超链接后跳转的结果

     

    第二种传值方式:

     

    点击登陆后跳转的结果。

     

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

     

    三、总结。

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

  • 相关阅读:
    UID卡、CUID卡、FUID卡的区别
    高中数学B版 高中数学A版
    Cenots7 服务搭建之搞清用户和组
    Flink 流处理 word count
    Flink Batch File Word Count
    Flink程序运行完yarn 模式后,返回运行standalone模式运行时。经常会出现运行不成功原因分析.
    Kafka消费者 API(自动提交offset)
    kafka 同步发送消息
    kafka 自定义分区
    kafka生产者 API Demo
  • 原文地址:https://www.cnblogs.com/zkycode/p/6273880.html
Copyright © 2011-2022 走看看