Springmvc参数传递

    •  

      参数传递注意是和界面打交道

      前端js页面数据传递给后端controller的过程

      新建项目springMVC6

      新建一个controller—DataController.java

      package com.tgb.web.controller.annotation;

      import javax.servlet.http.HttpServletRequest;

      import org.springframework.stereotype.Controller;

      import org.springframework.web.bind.annotation.RequestMapping;

      import org.springframework.web.bind.annotation.RequestMethod;

      import org.springframework.web.servlet.ModelAndView;

      @Controller

      @RequestMapping("/user/data")

      public class DataController {

              

          @RequestMapping("/addUser")

               public String adduser(String userName,String age,HttpServletRequest request){

                   request.setAttribute("userName",userName);

                   request.setAttribute("age",age);

                  

                         return "/userManager";

               }

              

          @RequestMapping("/delUser")

               public String delUser(){

                   String result = "this is delUser------";

                         return "";

               }

         

          @RequestMapping("/toUser")

          public String toUser(){

                   return "/addUser";

          }

      }

      根目录设置为/user/data:类前加@RequestMapping(“/user/data”)

      实现前端传递数据后,返回给界面

      新建一个view—addUser.js(get方式提交)

      主要是想把addUser的数据传到后台

      <h>添加用户</h>

      <form action=””>

               姓名:<input type=”text” name=”userName”>

               年龄:<input type=”text” name=”age”>

          <input type=”botton” value=”添加” onclick=”addUser()”>   

      </form>

      Javascript实现addUser功能

      <script type=”text/javascript”>

               function addUser(){

              var form=document.forms[0];

                         form.action=”/springMVC6/user/data/addUser”;

                   form.method=”get”;

                         form.submit();

      }

      </scrpit>

      【备注】

      这里可以不使用js,上面两部分可以直接用一个html代替即可:

      <h>添加用户</h>

          <form action="/springMVC6/user/data/addUser" method="get">

              姓名:<input type="text" name="userName"/>

              年龄:<input type="text" name="age"/>

             

              <input type="submit" value="添加">

          </form>

      现在看看如何去取这些表单

      把页面输入的数据显示到另一个页面

      Controller类修改

      第一种方法是,提供了参数解析方式。Name和参数名称匹配

      Name与标签的name相对应,这样就能获取到jsp传递过来的数据。

      package com.tgb.web.controller.annotation;

      import javax.servlet.http.HttpServletRequest;

      import org.springframework.stereotype.Controller;

      import org.springframework.web.bind.annotation.RequestMapping;

      import org.springframework.web.bind.annotation.RequestMethod;

      import org.springframework.web.servlet.ModelAndView;

      @Controller

      @RequestMapping("/user/data")

      public class DataController {

         

          @RequestMapping("/addUser")

          public String adduser(String userName,String age,HttpServletRequest request){

          request.setAttribute("userName", userName);

          request.setAttribute("age",age);

         

              return "/userManager";

          }

         

          @RequestMapping("/delUser")

          public String delUser(){

          String result = "this is delUser------";

              return "";

          }

         

          @RequestMapping("/toUser")

          public String toUser(){

          return "/addUser"; 

          }

      }

      其中userName与age与提交的userName、age相同

      新建另一个view作为提交页面—userManager.js

      DataController.java中addUser的view就定义了userManager,所以这里需要新建一个userManager.js,来显示表单提交的内容。

      把输入的数据返回到页面,

      新建一个页面:userManager.js页面

      <h1>用户管理</h1>

          姓名:======${userName }

          </br>

          年龄:======${age }

      修改addUserjs的数据提交方式,改为post提交

      addUser.js里面get方式改为Post方式提交

      post方法,乱码

      如何处理?

      Web.xml加入fileter过滤标签,这些可以在网上查找到

      <!--[if !supportLists]-->1.  <!--[endif]--><filter>  

      <!--[if !supportLists]-->2.  <!--[endif]-->        <filter-name>encodingFilter</filter-name>  

      <!--[if !supportLists]-->3.  <!--[endif]-->        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  

      <!--[if !supportLists]-->4.  <!--[endif]-->        <init-param>  

      <!--[if !supportLists]-->5.  <!--[endif]-->            <param-name>encoding</param-name>  

      <!--[if !supportLists]-->6.  <!--[endif]-->            <param-value>UTF-8</param-value>  

      <!--[if !supportLists]-->7.  <!--[endif]-->        </init-param>  

      <!--[if !supportLists]-->8.  <!--[endif]-->        <init-param>  

      <!--[if !supportLists]-->9.  <!--[endif]-->            <param-name>forceEncoding</param-name>  

      <!--[if !supportLists]-->10.<!--[endif]-->            <param-value>true</param-value>  

      <!--[if !supportLists]-->11.<!--[endif]-->        </init-param>  

      <!--[if !supportLists]-->12.<!--[endif]-->    </filter>  

      <!--[if !supportLists]-->13.<!--[endif]-->  

      <!--[if !supportLists]-->14.<!--[endif]-->    <filter-mapping>  

      <!--[if !supportLists]-->15.<!--[endif]-->        <filter-name>encodingFilter</filter-name>  

      <!--[if !supportLists]-->16.<!--[endif]-->        <url-pattern>/*</url-pattern>  

      <!--[if !supportLists]-->17.<!--[endif]-->    </filter-mapping>  

      上面一段代码,加入web.xml中

原文地址:https://www.cnblogs.com/exe19/p/5391671.html