Spring MVC的简单用法

一、Multiaction Controller

package cn.framelife.mvc.control;

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;

import cn.framelife.mvc.entity.User;

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

    /**
     * 这个方法接收/user/create.abc请求
     */
    @RequestMapping("create")
    public ModelAndView createUser(User user){
        System.out.println("createUser");

        ModelAndView view = new ModelAndView();
        view.setViewName("/success");

        return view;
    }

    /**
     * 这个方法接收/user/update.abc请求
     */
    @RequestMapping("update")
    public ModelAndView update(){
        System.out.println("updateUser");

        ModelAndView view = new ModelAndView();
        view.setViewName("/success");

        return view;
    }
}

二、 ModelAndView

Controller处理方法的返回值为ModelAndView,既包含视图信息,也包含模型数据信息。

1、ModelAndView中的方法
可以使用以下方法添加模型数据:

    addObject(Object modelObject)
    addObject(String modelName, Object modelObject)
    addAllObjects(Map modelMap)

可以通过以下方法设置视图:

    setViewName(String viewName) 
    setView(View view)

2、重定向:
这里的重定向只能重定向到其它的处理方法,不能重定向到页面。如果想重定向到页面,那么在另外的的处理方法中跳转就是。

    @RequestMapping(value="/add",method = RequestMethod.GET)
    public ModelAndView initForm(){
        User user = new User();
        return new ModelAndView("/add").addObject(user); 
    }

    @RequestMapping("create")
    public ModelAndView createUser(){
        ModelAndView view = new ModelAndView();

        //这里会重定向到add.abc的处理方法中,再由add.abc的处理方法作页面跳转
        RedirectView redirectView = new RedirectView("add.abc");
        view.setView(redirectView);

        return view;
    }

三、Controler处理方法获取值

1、获取页面值
页面Form表单:

<form action="user/create.abc" method="post">
        用户名:<input type="text" name="username"><br/>
        密 码:<input type="text" name="password"><br/>
        其它:<input type="text" name="other"><br/>
        <input type="submit">
</form>

A、 绑定到同名参数中

    /*
    * @RequestParam可以用来提取名为“username”的String类型的参数,并将之作为输入参数传入
    */
    @RequestMapping("create")
    public ModelAndView createUser(@RequestParam("username") String username,String password,String other){
        System.out.println(username+"-"+password+"-"+other);
        ModelAndView view = new ModelAndView();
        view.setViewName("/success");

        return view;
}

B、 绑定到实体类模型数据中
User实体类:

public class User implements java.io.Serializable {
    private Integer id;
    private String username;
    private String password;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

Controller中的方法:

    @RequestMapping("create")
    public ModelAndView createUser(User user){
        System.out.println(user.getUsername()+"-"+user.getPassword());
        ModelAndView view = new ModelAndView();
        view.setViewName("/success");

        return view;
    }

C、既有模型又有同名参数

    /**
     * 页面表单值传输时,如果User类中有同名的属性,那就绑定到User对象中
     * 如果User类中没有的属性,可以使用同名参数接收
     * 如果User类中也有,而我们又有同名参数,两个都可以接收
     */
    @RequestMapping("create")
    public ModelAndView createUser(User user,String username,String other){
        System.out.println(user.getUsername()+"-"+user.getPassword()+"-"+username+"-"+other);
        ModelAndView view = new ModelAndView();
        view.setViewName("/success");

        return view;
    }

2、获取Servlet API
Controller中的处理方法:

    @RequestMapping("create")
    public ModelAndView createUser(HttpServletRequest request,HttpServletResponse response,HttpSession session){
        String username = request.getParameter("username");
        System.out.println(username);

        request.setAttribute("requestName", "aaaaaaaaaaaa");
        session.setAttribute("sessionName", "bbbbbbbbbbbb");

        Cookie cookie = new Cookie("cookieName", "ccccccccccc");
        response.addCookie(cookie);

        ModelAndView view = new ModelAndView();
        view.setViewName("/success");

        return view;
    }

Success.jsp页面显示:

<body>
    ${requestName}<br/>
    ${sessionName}<br/>
    ${cookie.cookieName.value}<br/>
</body>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值