加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:13:43 所属栏目:大数据 来源:网络整理
导读:如题,这个是以前做的笔记,现在搬到博客上...... package com.ruide.action;? import java.util.HashMap; java.util.Map;? javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest;? org.springframework.http.HttpRequest; org.springfra

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

package com.ruide.action;
?
import java.util.HashMap;
 java.util.Map;
?
 javax.servlet.http.HttpServlet;
 javax.servlet.http.HttpServletRequest;
?
 org.springframework.http.HttpRequest;
 org.springframework.stereotype.Controller;
 org.springframework.ui.ModelMap;
 org.springframework.web.bind.annotation.ModelAttribute;
 org.springframework.web.bind.annotation.RequestMapping;
 org.springframework.web.bind.annotation.RequestParam;
 org.springframework.web.servlet.ModelAndView;
?
 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" 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+" "+方法3:通过对象来接受值(该方法需要控件name与对象属性一致)
    @RequestMapping("/login.do" String login(User user){
        
        System.out.println(user.getUsername()+" "+user.getUserpass());
        
        方法4:通过与控件name同名的变量接受值
    @RequestMapping("/login.do" String login(String username,String userpass){
        
        System.out.println(userpass+" "+username);
        
        return "index";
    }
    
    
    
    
     * ----------------------如何把值传递到页面----------------------
     * 
     * 方法1:通过request把值传递到页面
    @RequestMapping("/login.do" String login(User user,HttpServletRequest request){
        
        request.setAttribute("username",user.getUsername());
        request.setAttribute("userpass"方法2:通过框架自带的modelmap集合传递到页面
    @RequestMapping("/login.do"方法3:通过框架自带的model and view传递值(常用)
    @RequestMapping("/login.do" 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"设置要转发的页面
        mv.setViewName("index");
        return mv;
    }
    
    方法4:通过注解传递值(注解中的名字会被赋值)
    注意:注解过的方法会在整个action接受到请求时最先执行(不推荐使用)
    @ModelAttribute("name" String getName(){
        return "haha";
    }
}

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读