传递模型数据到视图中
(1)定义数据层
import java.util.List; import entity.Spittle; public interface Repository { List<Spittle> findSpittles(long max, int count); int save(Spittle spittle); Spittle findByUserName(String userName); }
实现类
import java.util.ArrayList; import java.util.Date; import java.util.List; import org.springframework.stereotype.Component; import entity.Spittle; @Component("repository")//为该类命名为repository public class RepositoryImpl implements Repository { @Override public List<Spittle> findSpittles(long max, int count) { Date date = new Date(); Spittle spittle = new Spittle("1", date); Spittle spittle1 = new Spittle("2", date); Spittle spittle2 = new Spittle("3", date); Spittle spittle3 = new Spittle("4", date); List<Spittle> list = new ArrayList<>(); list.add(spittle); list.add(spittle1); list.add(spittle2); list.add(spittle3); return list; } @Override public int save(Spittle spittle) { return 0; } @Override public Spittle findByUserName(String userName) { Date date = new Date(); Spittle spittle = new Spittle("1", date); spittle.setUserName(userName); return spittle; } }
实体类
import java.util.Date; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; public class Spittle { private Long id; private String message; private Date time; private Double latitude; private Double longitude; private String userName; private String firstName; private String lastName; private String password; public Spittle() { super(); } public Spittle(String message, Date time) { this(message, time, null, null); } public Spittle(String message, Date time, Double latitude, Double longitude) { this.message = message; this.time = time; this.latitude = latitude; this.longitude = longitude; } public Spittle(String userName, String firstName, String lastName, String password) { this.userName = userName; this.firstName = firstName; this.lastName = lastName; this.password = password; } public Double getLatitude() { return latitude; } public void setLatitude(Double latitude) { this.latitude = latitude; } public Double getLongitude() { return longitude; } public void setLongitude(Double longitude) { this.longitude = longitude; } public Long getId() { return id; } public String getMessage() { return message; } public Date getTime() { return time; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public boolean equals(Object that) { //EqualsBuilder 类需要引入 apache的commons-lang包 return EqualsBuilder.reflectionEquals(this, that, "id", "time"); } public int hashCode() { return HashCodeBuilder.reflectionHashCode(this, "id", "time"); } }
接受数据的控制器
import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; 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 data.Repository; import entity.Spittle; @Controller @RequestMapping("/spittles") public class SpittleController { //private static final String MAX_LONG_AS_STRING = Long.toString(Long.MAX_VALUE); private Repository repository; @Autowired public SpittleController(Repository repository) {//注入 Repository this.repository = repository; } // @RequestMapping(method=RequestMethod.GET) // public String spittles(Model model) { // model.addAttribute("spittleList", repository.findSpittles(Long.MAX_VALUE, 20)); //将 spittle 添加到模型中 // return "spittles"; //返回视图名 // } // @RequestMapping(method=RequestMethod.GET) // public String spittles(Map model) {//这里也可以用 Map 作为参数 // model.put("spittleList", repository.findSpittles(Long.MAX_VALUE, 20)); //将 spittle 添加到模型中 // return "spittles"; //返回视图名 // } // @RequestMapping(method=RequestMethod.GET) // public List<Spittle> spittles() { // return repository.findSpittles(Long.MAX_VALUE, 20); // } //接受请求的输入 @RequestMapping(method=RequestMethod.GET) public List<Spittle> spittles(@RequestParam(value="max", defaultValue="30") long max, @RequestParam(value="count", defaultValue="20") int count) { System.out.println("pppppp"); return repository.findSpittles(Long.MAX_VALUE, 20); }
//通过路径参数接受输入
通过使用@RequestParam注解,让它接受ID作为查询参数 这个处理器方法会将如 /spittles/show?id=1234这样的请求 @RequestMapping(value="show", method=RequestMethod.GET) public String showSpittle(@RequestParam("id") long id, Model model) { System.out.println("show"); return "spittles"; }
//但是我们想要识别的资源通过URL路径进行标识,而不是通过查询参数 这种路径变量 SpringMVC允许我们在@RequestMapping路径中使用占位符
//下面的可以处理对 /spittles/12345 的请求 ,12345会传递到 id 这个参数中 标为黄色的两属性名称要一致,如果参数名与占位符的名称相同,则可以将@PathVariable("spittleId") 简写为 @PathVariable
//如果@PathVariable中没有 value 属性的话,它会假设占位符的名称与方法的参数名称相同 @RequestMapping(value="/{spittleId}", method=RequestMethod.GET) public String spittle(@PathVariable("spittleId") long id, Model model) { System.out.println("" + id); return "spittles"; } }
返回的视图
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> hello c <c:forEach items="${spittleList}" var="spittle"> <li id="spittle_<c:out value="spittle.id"/>"> <div class="spittleMessage"> <c:out value="${spittle.message}"></c:out> </div> <div> <span class="spittleTime"> <c:out value="${spittle.time}" /> </span> <span class="spittleLocation"> (<c:out value="${spittle.latitude}"/> <c:out value="${spittle.longitude}"/>) </span> </div> </li> </c:forEach> </body> </html>