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>
posted @ 2017-05-11 18:25  mfmdaoyou  阅读(234)  评论(0编辑  收藏  举报