springMVC 传递参数和回传数据

1、基本数据类型+String上传:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<body>
<h2>Hello World!</h2>
<%
String path = request.getContextPath();
%>
<a href="<%=request.getContextPath()%>/user/delete.do?id=10015"> delete </a>

<form action="<%=path %>/user/login.do" method="post">
    账号:<input type="text" name="account"><br>
    密码:<input type="password" name="pwd"><br>
    账号:<input type="submit" value="提交"><br>

</form>
</body>
</html>

Controller写法:

package com.xingxue.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.xingxue.service.UserService;

@Controller
@RequestMapping("/user")
public class UserContrller {

    @Autowired
    public UserService service;


    public ModelAndView userLogin1(String account, String pwd) {

        System.out.println("canshu : " + account);
        System.out.println("canshu : " + pwd);


        ModelAndView mv = new ModelAndView();
        mv.setViewName("user/ok");
        mv.addObject("msg", "ok!!!!!!");
        return mv;
    }

}

测试通过
复杂数据类型,实体对象作为参数传递:

@RequestMapping("/login2.do")
    public ModelAndView userLogin2(User user) {

        System.out.println("canshu : " + user.getAccount());
        System.out.println("canshu : " + user.getPwd());

        ModelAndView mv = new ModelAndView();
        mv.setViewName("user/ok");
        mv.addObject("msg", "ok!!!!!!");
        return mv;
    }
页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<body>
<h2>Hello World!</h2>
<%
String path = request.getContextPath();
%>
<a href="<%=request.getContextPath()%>/user/delete.do?id=10015"> delete </a>

<form action="<%=path %>/user/login2.do" method="post">
    账号:<input type="text" name="account"><br>
    密码:<input type="password" name="pwd"><br>
    账号:<input type="submit" value="提交"><br>

</form>
</body>
</html>

此处注意:区别与struts2, struts2 提交参数是对象名.属性名 ,springMVC是直接属性名,springMVC会自动到该对象获取该属性并且注入数据。

HashMap接收参数

此处需要注意:@RequestParam注解,表示吧参数全部注入到map对象里面

@RequestMapping("/login3.do")
    public ModelAndView userLogin3(@RequestParam HashMap<String, Object> param) {

        System.out.println(param.get("account"));
        System.out.println(param.get("pwd"));

        ModelAndView mv = new ModelAndView();
        mv.setViewName("user/ok");
        mv.addObject("msg", "ok!!!!!!");
        return mv;
    }
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<body>
<h2>Hello World!</h2>
<%
String path = request.getContextPath();
%>
<a href="<%=request.getContextPath()%>/user/delete.do?id=10015"> delete </a>

<form action="<%=path %>/user/login3.do" method="post">
    账号:<input type="text" name="account"><br>
    密码:<input type="password" name="pwd"><br>
    账号:<input type="submit" value="提交"><br>

</form>
</body>
</html>

springMVC 获取servletAPI对象

@RequestMapping("/login3.do")
    public ModelAndView userLogin3(@RequestParam HashMap<String, Object> param,
                                    HttpSession session,
                                    HttpServletRequest request,
                                    HttpServletResponse response
            ) {
        ModelAndView mv = new ModelAndView();


        HashMap<String, Object> loginUser = this.service.userLogin(param);
        if(loginUser == null) {
            mv.addObject("msg", "NO!!!!!!");
            mv.setViewName("index");
        } else {
            session.setAttribute("loginUser", loginUser);
            mv.addObject("msg", "OK!!!!!!");
            mv.setViewName("user/ok");
        }

        return mv;
    }

springMVC如果需要获取servletAPI ,只需要在方法的参数里面定义该类型的参数,springMVC就会自动注入一个该类型对象给我们。

参数回传:

ModelAndView mv = new ModelAndView();

        HashMap<String, Object> loginUser = this.service.userLogin(param);
        if(loginUser == null) {
            mv.addObject("msg", "NO!!!!!!");
            mv.setViewName("../index");
        } else {
            session.setAttribute("loginUser", loginUser);
            mv.addObject("msg", "OK!!!!!!");
            mv.setViewName("user/ok");
        }

springMVC提供了一个modelAndView的类,这个对象封装了我们的视图信息和回传数据信息,想要回传数据,直接利用该对象的addObejct存入即可,这样我们的数据会存入request对象,然后跳转页面,所以页面直接从request获取即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值