The request and response SpringMVC

Foreword

Earlier we learned the basic configuration SpringMVC how next a very important knowledge is to accept the user's request, and how to send data to the user.

 

Get request parameters

Page parameters obtained in several ways

1) obtained by the parameter name

The method of setting parameters to the controller of the same name and the form name

2) NOTE Setting parameters @RequestParam ( "parameter name")

@RequestParam ( "form element name") parameter type parameter name

3) automatic packing, attribute names and forms to create the name of the same class

The method of the class as a parameter

 

Obtained by the parameter name

Page form:

<form action=”/user/login” method=”post”>

<input type=”text” name=”username”><br>

<input type=”password” name=”password”><br>

<input type=”submit” value=”登录”><br>

</form>

Controller:

@Controller

@RequestMapping(“/user”)

public class UserController{

 

@RequestMapping(“/login”)

public String login(String username,String password){

System.out.println(“username : ” + username);

System.out.println(“password: ” + password);

return “index”;

}

}

Note: This parameter names and login method must be consistent with the name attribute of the form element

 

NOTE Setting parameters @RequestParam ( "parameter name")

Controller:

@Controller

@RequestMapping(“/user”)

public class UserController{

 

@RequestMapping(“/login”)

public String login(@RequestParam(“username”)String name,

@RequestParam(“password”)String pwd){

System.out.println(“username : ” + name);

System.out.println(“password: ” + pwd);

return “index”;

}

}

Note: This parameter names the login method must be consistent with and does not require the name attribute of the form element, only you need to set the name of the form element in front of the parameter annotation @RequestParam on it.

 

Autoboxing, attribute names and forms to create the name of the same class

User-defined categories:

public class User{

private String username;

private String password;

omitted get, set method

}

Controller:

@Controller

@RequestMapping(“/user”)

public class UserController{

 

@RequestMapping(“/login”)

public String login(User user){

System.out.println(“username : ” + user.getName());

System.out.println(“password: ” + user.getPassword());

return “index”;

}

}

Note: Parameters login method uses a custom class that the attribute name must be the same form element name, you must provide the get and set methods.

 

Data back to the page

Earlier we learned how to get data from the page, let's learn how to return the data to the page.

1) collection by Map

In the method parameters added Map set, the data set into

2) Model Model

In the method parameters added Model parameters, use of the addAttribute Model ( "name", value) add data

3) through the object request, Session Object

Add HttpServletRequest parameter or parameters in the method HttpSession

Map collection by

To bind data using an EL expression in the index.jsp page:

<H2> Hello, welcome $ {username} </ h2>

Controller:

@Controller

@RequestMapping(“/user”)

public class UserController{

 

@RequestMapping(“/login”)

public String login(@RequestParam(“username”)String name,

@RequestParam(“password”)String pwd,Map<String,String> map){

System.out.println(“username : ” + name);

System.out.println(“password: ” + pwd);

map.put(“username”,name);

return “index”;

}

}

Note: Map as a parameter set can be placed anywhere login process, the key is stored in the collection will be a username EL expression $ {username} corresponding to the name, the stored set value is displayed on the page.

Model by model

@Controller

@RequestMapping(“/user”)

public class UserController{

 

@RequestMapping(“/login”)

public String login(@RequestParam(“username”)String name,

@RequestParam(“password”)String pwd,Model model){

System.out.println(“username : ” + name);

System.out.println(“password: ” + pwd);

model.addAttribute(“username”,name);

return “index”;

}

}

Note: Use the object here is to Mode, similar to the role of this object Map collection front, but it is designed to hold the data, then send it to the page, the actual development with more.

 

By request object, Session Object

Controller:

@Controller

@RequestMapping(“/user”)

public class UserController{

 

@RequestMapping(“/login”)

public String login(@RequestParam(“username”)String name,

@RequestParam(“password”)String pwd,HttpServletRequest req){

System.out.println(“username : ” + name);

System.out.println(“password: ” + pwd);

req.setAttribute(“username”,name);

return “index”;

}

}

NOTE: As used herein, the commonly JavaWeb HttpServletRequest request object, for transmitting data to a page, may also be used HttpSession object, HttpSession objects of all components in the user shared memory object stored in a long time, so try use HttpServletRequest, a more efficient use of memory.

 

 

to sum up

In this chapter we learned several ways to get data from the controller page, and return the data from the controller to the ways of the page. In this chapter we have mastered the basic method SpringMVC operating data, combined with the database we will be able to achieve a basic JavaWeb project.

 

Guess you like

Origin www.cnblogs.com/qfchen/p/11352252.html