首先看項目目錄:
創建user實體:
package spittr;
import java.util.Date;
public class User {
private String name;
private String password;
private Date time;
public User(){}
public User(String name, String password, Date time){
this.name = name;
this.password = password;
this.time = time;
}
spittr.inte包下存儲的是接口文件
package spittr.inte;
import java.util.List;
import spittr.User;
public interface UserInte {
List<User> findUsers(int count);
}
具體的實現在spittr.impl包下,爲了能夠讓Spring自動聲明爲一個bean要在實現類上加@Component註解
package spittr.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Component;
import spittr.User;
import spittr.inte.UserInte;
@Component
public class UserImpl implements UserInte {
@Override
public List<User> findUsers(int count) {
List<User> list = new ArrayList<User>();
for(int i=0; i<count; i++){
User user = new User("user_"+i, i+"", new Date());
list.add(user);
}
return list;
}
}
寫UserController類,
SpringMvc允許以多種方式將客戶端中的數據傳送到控制器的處理方法中,包括:
- 查詢參數
- 表單參數
- 路徑變量
package spittr.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import spittr.User;
import spittr.inte.UserInte;
@Component
@RequestMapping("/users")
public class UserController {
@Autowired
private UserInte userInte;
@RequestMapping(method=RequestMethod.GET)
public String users(Model model){
model.addAttribute("userList", userInte.findUsers(20));
return "users";
}
/**
* 1、接收查詢參數
* @param count
* @param model
* @return
*/
@RequestMapping(value="/param", method=RequestMethod.GET)
public String userList(@RequestParam(value="count", defaultValue="20") int count, Model model){
model.addAttribute("userList", userInte.findUsers(count));
return "users";
}
/**
* 2、接收路徑變量參數
* 如果方法的參數名與佔位符的名稱相同,可以去掉@PathVariable中的value屬性
* 即 public String userList2(@PathVariable int count, Model model){
* @param count
* @param model
* @return
*/
@RequestMapping(value="/{count}", method=RequestMethod.GET)
public String userList2(@PathVariable("count") int count, Model model){
model.addAttribute("userList", userInte.findUsers(count));
return "users";
}
@RequestMapping(value="/register", method=RequestMethod.GET)
public String register(){
return "register";
}
/**
* 3、接收表單參數
* @param user
* @return
*/
@RequestMapping(value="/register", method=RequestMethod.POST)
public String regist(User user){
System.out.println(user);
System.out.println("用戶註冊的邏輯--------");
return "redirect:/users";
}
}
兩個頁面:
users.jsp頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!-- 屏蔽tomcat 自帶的 EL表達式 -->
<%@ page isELIgnored="false"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Users</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<table cellspacing="10px" cellpadding="10px" align="center"
style="background-color: silver;">
<c:forEach items="${userList}" var="user">
<tr>
<td><c:out value="${user.name}" /></td>
<td><c:out value="${user.password}" /></td>
<td><c:out value="${user.time}" /></td>
</tr>
</c:forEach>
</table>
</body>
</html>
register.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!-- 屏蔽tomcat 自帶的 EL表達式 -->
<%@ page isELIgnored="false"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Users</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="" method="post"><!-- 這裏沒有寫action jsp會默認提交到展現頁面的url -->
<table cellspacing="10px" cellpadding="10px" align="center"
style="background-color: silver;">
<tr>
<td><input type="text" name="name" placeholder="name" /></td>
</tr>
<tr>
<td><input type="text" name="password" placeholder="password" /></td>
</tr>
<!-- <tr>
<td><input type="date" name="time" placeholder="time" /></td>
</tr> -->
<tr>
<td><button type="submit">提交</button></td>
</tr>
</table>
</form>
</body>
</html>
如果在編譯的時候出現No qualifying bean of type 'xxxx' available 則是沒有把實體注入進去,修改RootConfig和WebConfig類的@ComponentScan("spittr")
如果在提交表單的時候出現The request sent by the client was syntactically incorrect,那麼修改前端和後端接收的字段類型要一致,不一致spring就會報錯。