参考入门案例:Spring MVC入门案例!!!-CSDN博客
1.接收基本数据类型:通常用于参数较少的情况。
在index.jsp中定义超链接:
html
<a href="/account/findAccount7?name=mike&age=18">接收基本数据类型</a>
编写AccountController:
java
@Controller
@RequestMapping("/account")
public class AccountController {
@GetMapping("/findAccount7")
public String findAccount7(String name, Integer age, Model model) throws UnsupportedEncodingException {
//get请求解决乱码问题
byte[] bytes = name.getBytes(StandardCharsets.ISO_8859_1);
name = new String(bytes, StandardCharsets.UTF_8);
model.addAttribute("msg", "通过url传参:" + name + "---" + age);
return "success";
}
}
2.通过pojo实体类进行接收:通常用于参数较多的情况。
实体类:Account
java
package com.by.pojo;
import java.io.Serializable;
import java.util.Date;
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
private Address address;
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
//省略get set toString方法
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
", address=" + address +
", date=" + date +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
实体类:Address
java
package com.by.pojo;
import java.io.Serializable;
public class Address implements Serializable {
private String provinceName;
private String cityName;
//省略get set toString方法
@Override
public String toString() {
return "Address{" +
"provinceName='" + provinceName + '\'' +
", cityName='" + cityName + '\'' +
'}';
}
public String getProvinceName() {
return provinceName;
}
public void setProvinceName(String provinceName) {
this.provinceName = provinceName;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
}
编写AccountController
java
package com.by.controller;
import com.by.pojo.Account;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/account")
public class AccountController {
@RequestMapping("/saveAccount")
public String saveAccount(Account account, Model model){
model.addAttribute("msg", account);
return "success";
}
}
在index.jsp中添加表单:
html
<form action="account/saveAccount" method="post">
账户名称:<input type="text" name="name"><br/>
账户金额:<input type="text" name="money"><br/>
账户省份:<input type="text" name="address.provinceName"><br/>
账户城市:<input type="text" name="address.cityName"><br/>
<input type="submit" value="保存">
</form>
3.采用restful风格的方式进行接收
在index.jsp中添加超链接
html
<br><a href="/account/findAccount10/小美/18">restful</a><br>
在AccountController中编写方法:通过在参数列表中使用@PathVariable
java
/*
* Copyright (c) 2020, 2024, All rights reserved.
*
*/
package com.by.controller;
import com.by.pojo.Account;
import com.by.pojo.JsonStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
/**
* <p>Project: SpringMVC - AccountController</p>
* <p>Powered by scl On 2024-01-09 10:07:21</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
@Controller
@RequestMapping("/account")
public class AccountController {
@GetMapping("/findAccount10/{name}/{age}")
public String findAccount10(@PathVariable String name, @PathVariable Integer age, Model model) {
byte[] bytes = name.getBytes(StandardCharsets.ISO_8859_1);
name = new String(bytes, StandardCharsets.UTF_8);
model.addAttribute("msg", "restful:" + name + ":" + age);
return "success";
}
}
在Restful之前的操作:
http://127.0.0.1/user/query?id=1 根据用户id查询用户数据
RESTful用法:
http://127.0.0.1/user/1 GET 根据用户id查询用户数据
http://127.0.0.1/user POST 新增用户
http://127.0.0.1/user PUT 修改用户信息
http://127.0.0.1/user/1 DELETE 删除用户信息
RESTful总结:
Restful风格就是请求url统一,根据不同的请求方式,请求不同的后台方法。如果需要携带参数,在url上使用/{}占位符。
4.使用ServletAPI接收参数
编写controller
java
@Controller
@RequestMapping("/account")
public class AccountController {
@RequestMapping("/findAccount8")
public String findAccount8(HttpServletRequest request,
HttpServletResponse response){
String username = request.getParameter("name");
String age = request.getParameter("age");
request.setAttribute("msg",username+" "+age);
return "success";
}
}
在index.jsp里面定义超链接
html
<a href="/account/findAccount8?username=eric&age=19">Servlet接收参数</a>