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

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

概述如题,这个是以前做的笔记,现在搬到博客上...... package com.ruide.action; ​ import java.util.HashMap; import java.util.Ma 如题,这个是以前做的笔记,现在搬到博客上......
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管理类@Controllerpublic 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";    }}

 

总结

以上是内存溢出为你收集整理的在SpringMVC中,jsp和后台互相传值全部内容,希望文章能够帮你解决在SpringMVC中,jsp和后台互相传值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1217452.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存