SpringMVC return type

7.SpringMVC return type and parameter passing

. 1 , SPRINGMVC return type

(. 1) ModelAndView Return Value Type:

  1.1 When the return is null , the page does not jump.

  1.2 when the return value does not specify the name of the view, default jump request name as the name of the view.

  1.3 when the return value specifies the name of the view, the program will jump by view name.

 

/*添加*/
@RequestMapping("/getSale")
public ModelAndView addSale(Sale sale,HttpServletRequest request,ModelAndView mv){
    if (sale!=null) {
        Double totalPrice = sale.getPrice() * sale.getQuantity();
        sale.setTotalPrice(totalPrice);
        sale.setSaleDate(new Date());
        Users user = (Users) request.getSession().getAttribute("user");
        sale.setUserId(user.getUid());
        int i = userService.addSale(sale);
        if (i > 0) {
            mv.setViewName("saleList");
        } else {
            mv.setViewName("prodectAdd");
        }
    }
    return mv;
}

 

(2) Object Return Value Type :

/*绑定下拉框*/
@RequestMapping("/prodectName")
@ResponseBody
public Object getprodectName(){
    List<Product> products = userService.getproductName();
    return products;
}

(. 3) String Return Value Type:

3.1 If the returned value is null, then a jump to request the name of the view name;

3.2 If the return value is specified, then a jump according to the specified view name as the return value, may carry data model, modeMap.

3.3 If the value returned with forward or redirect prefix, or the corresponding request will be redirected, but can not carry data mvc data model, the data may be carried by ServletApi.

@RequestMapping ( "/ available for purchase" )
 public String available for purchase (String the userName, the Model Model) {
     // save the user name corresponding to the scope 
    model.addAttribute ( "the userName" , the userName);
     return "available for purchase" ; 
}

2. parameter passing

(. 1) the JSP page ( note point *: Controller Controller method parameter name and must form element name consistent with the property value )

<form class="loginForm" action="/user/getUser" method="post" >
    <div class="inputbox"  style="text-align:center; ">
        <label for="user">用户名:</label>
        <input id="user" type="text" name="userName" placeholder="请输入用户名" />
    </div>
    <div class="password"  style="text-align:center; " >
        <label for="mima">密码:</label>
        <input id="mima" type="password" name="password" placeholder="请输入密码" />
    </div>
    <div class="subBtn"  style="text-align:center; ">
        <input type="submit" value="登录" />
        <input type="reset" value="重置"/>
    </div>
</form>
/*登录*/
@RequestMapping("/getUser")
@ResponseBody
private ModelAndView getUser(String userName, String password, ModelAndView mv, HttpServletRequest request, HttpServletResponse response, HttpSession session){
    Users user = userService.getUser(userName,password);
    System.out.println("user======"+user);
    if (user!=null){
        System.out.println("成功");
        //登录成功
      request.getSession().setAttribute("user",user);
        //转发
        mv.setViewName("index"); 
    } The else {
         // login failed 
        mv.setViewName ( "Login" ); 
    } 
    return Music Videos; 
}

(2) request for the parameter fitting POJO objects

New Person

public class Person {
    private String username;
    private int age;
    //省略get/set方法
}

Controller

// When the entity name attribute class attribute names and forms of the same element, to complete the automatic assembly 
@RequestMapping (value = "personObject", Method = RequestMethod.POST)
 public String personObject (the Person Person) { 
    System.out.println (Person); 
    return "Hello" ; 
}

(3) @RequestParam comment

@RequestParam effect that in the case where the controller parameter elements and the form does not match a method using annotations @RequestParam declared parameter name.

@RequestParam has three attributes:

  3.1value: request parameter name (must be configured)

  3.2required: Required, default is true, i.e., the request must include the parameter, if it does not, an exception is thrown (optional)

  3.3defaultValue: default value if the value is set, required automatically set to false, regardless of whether you configure required, configure what values ​​are false (optional)

 jsp page

<form class="loginForm" action="/getUser" method="post" onsubmit="return check()" >
    <div class="inputbox"  style="text-align:center; ">
        <label for="user">用户名:</label>
        <input id="user" type="text" name="userName" placeholder="请输入用户名" />
    </div>
    <div class="password"  style="text-align:center; " >
        <label for="mima">密码:</label>
        <input id="mima" type="password" name="password"placeholder = "Please enter your password" /> 
        <= the INPUT of the type "the Submit" value = "Login" />= "SUBBTN" style = "text-align = left: Center;">class
    <div
    </ div>
        <INPUT type = "RESET" value = "reset" />
    </div>
</form>

Controller

<form class="loginForm" action="/getUser" method="post" onsubmit="return check()" >
    <div class="inputbox"  style="text-align:center; ">
        <label for="user">用户名:</label>
        <input id="user" type="text" name="userName" placeholder="请输入用户名" />
    </div>
    <div class="password"  style="text-align:center; " >
        <label for="mima">密码:</label>
        <input id="mima" type="password" name="password"placeholder = "Please enter your password" /> 
        <= the INPUT of the type "the Submit" value = "Login" />= "SUBBTN" style = "text-align = left: Center;">class
    <div
    </ div>
        <INPUT type = "RESET" value = "reset" />
    </div>
</form>

(. 4) RESTFUL style parameter passing

/*{id}表示占位符*/
@RequestMapping("/getid/{id}")
@ResponseBody
public Object getid(@PathVariable("id") Integer id){
    List<Accounts> name = accountService.getName(id);
    return name;
} 

(5) an object transfer parameters


IUserInfo entity classes:

package com.cmy.entity;
import java.util.ArrayList;
import java.util.List;
public class IUserInfo {
    private  int uid;
    private String username;
    //域属性注入
    private Teacher teacher;
    private List<Teacher> teacherList;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public int getUid() {
        return uid;
    }
    public void setUid(int uid) {
        this.uid = uid;
    }
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    public List<Teacher> getTeacherList() {
        return teacherList;
    }

    public void setTeacherList(List<Teacher> teacherList) {
        this.teacherList = teacherList;
    }
    @Override
    public String toString() {
        return "IUserInfo{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", teacher=" + teacher +
                ", teacherList=" + teacherList +
                '}';
    }
}

Control Layer

@Controller
@RequestMapping("/fout")
public class FoutController {//属性
    @RequestMapping("/getUser")
    public String getUser(IUserInfo userInfo){
        System.out.println(userInfo.toString());
        return "welcome";
    }
}

page

Domain Properties 1

<form class="loginForm" action="/fout/getUser" method="post" onsubmit="return check()" > 
<div class="inputbox" style="text-align:center; ">
 <label for="user">用户名:</label>
 <input id="user" type="text" name="teacher.teachername" placeholder="请输入用户名" /> 
<input id="users" type="text" name="teacher.teachername" placeholder="请输入用户名" />
 </div> 
<div class="password" style="text-align:center; " > 
<label for="mima">密码:</label> 
<input id="mima" type="password" name="password" placeholder="请输入密码" /> 
</div>
 <div class="subBtn" style="text-align:center; "> 
<input type="submit" value="登录" />
 <input type="reset" value="重置"/> 
</div> 
</form>

Set of 2

<form class="loginForm" action="/fout/getUser" method="post" onsubmit="return check()" >
            <div class="inputbox"  style="text-align:center; ">
                <label for="user">用户名:</label>
                <input id="user" type="text" name="teacherList[0].teachername" placeholder="请输入用户名" />
                <input id="users" type="text" name="teacherList[1].teachername" placeholder="请输入用户名" />
            </div>
            <div class="password"  style="text-align:center; " >
                <label for="mima">密码:</label>
                <input id="mima" type="password" name="password" placeholder="请输入密码" /></div>
            <div class="subBtn"  style="text-align:center; ">
                <input type="submit" value="登录" />
                <input type="reset" value="重置"/>
            </div>
        </form>

 

 

Guess you like

Origin www.cnblogs.com/ws1149939228/p/11830473.html