在SpringMVC中,jsp和后台互相传值

本文详细介绍了在Spring MVC框架中,如何通过不同的方式从页面获取参数并返回数据到视图。包括了使用HttpServletRequest、@RequestParam、User对象及直接指定参数名等多种接收参数的方法,以及通过request、ModelMap、ModelAndView和@ModelAttribute等进行数据传递的技术要点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如题,这个是以前做的笔记,现在搬到博客上......

package com.ruide.action;
​
import java.util.HashMap;
import java.util.Map;
​
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
​
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
​
import com.ruide.po.User;
​
//让spring管理类
@Controller
public class TestAction {
    //设置请求路径
    @RequestMapping(value="/hello.do")
    public String say(){
        System.out.println("Hello World");
        
        //return "index";//默认请求转发
        
        return "redirect:/index.jsp";
    }
    
    /*
     * ----------------------如何从页面里获取值----------------------
     * 
     * */
    
    //方法1:使用request接受参数
    @RequestMapping("/login.do")
    public String login(HttpServletRequest request){
        String username=request.getParameter("username");
        String userpass=request.getParameter("userpass");
        System.out.println(username+userpass);
        
        return null;
    }
    
    //方法2:直接通过注解在参数中获取值
    @RequestMapping("/login.do")
    public String login(@RequestParam("username") String username,
                        @RequestParam("userpass") String userpass){
        
        System.out.println(username+" "+userpass);
        
        return null;
    }
    
    //方法3:通过对象来接受值(该方法需要控件name与对象属性一致)
    @RequestMapping("/login.do")
    public String login(User user){
        
        System.out.println(user.getUsername()+" "+user.getUserpass());
        
        return null;
    }
    
    //方法4:通过与控件name同名的变量接受值
    @RequestMapping("/login.do")
    public String login(String username,String userpass){
        
        System.out.println(userpass+" "+username);
        
        return "index";
    }
    
    
    
    /*
     * ----------------------如何把值传递到页面----------------------
     * 
     * */
    
    //方法1:通过request把值传递到页面
    @RequestMapping("/login.do")
    public String login(User user,HttpServletRequest request){
        
        request.setAttribute("username",user.getUsername());
        request.setAttribute("userpass", user.getUserpass());
        
        return "index";
    }
    
    //方法2:通过框架自带的modelmap集合传递到页面
    @RequestMapping("/login.do")
    public String login(User user,ModelMap mm){
        
        mm.put("username", user.getUsername());
        mm.put("userpass", user.getUserpass());
        
        return "index";
    }
    
    //方法3:通过框架自带的model and view传递值(常用)
    @RequestMapping("/login.do")
    public ModelAndView login(User user){
        //把值放入一个键值对中
//      Map<String,String> model=new HashMap<String,String>();
//      model.put("username", user.getUsername());
//      ModelAndView mv=new ModelAndView("index",model);
        //把对象直接放入键值对中
        ModelAndView mv=new ModelAndView();
        mv.addObject("user",user);
        //设置要转发的页面
        mv.setViewName("index");
        return mv;
    }
    
    //方法4:通过注解传递值(注解中的名字会被赋值)
    //注意:注解过的方法会在整个action接受到请求时最先执行(不推荐使用)
    @ModelAttribute("name")
    public String getName(){
        return "haha";
    }
}

 

转载于:https://www.cnblogs.com/Createsequence/p/11228889.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值