参考答案
Spring Web MVC的工作流程如下:
1. 浏览器发出spring mvc请求,请求交给前端控制器DispatcherServlet处理。
2. 控制器通过HandlerMapping维护的请求和Controller映射信息,找到相应的Controller组件处理请求。
3. 执行Controller组件约定方法处理请求,在约定方法中可以调用Service和DAO等组件完成数据库操作。约定方法可以返回一个ModelAndView对象,封装了模型数据和视图名称信息。
4. 控制器接收ModelAndView之后,调用ViewResolver组件,定位View的JSP并传递Model信息,生成响应界面结果。
1) City城市类有id和name两个属性;
2)自行构建List<City>集合,并存入自定义的数据,不需要从数据库查询;
3)在JSP中可以采用JSTL和EL表达式显示集合数据。
参考答案
图– 1
步骤二:在src下添加Spring Web MVC的XML配置文件
代码如下所示:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> </beans>
步骤三:在web.xml中配置DispatcherServlet前端控制器组件
代码如下所示:
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> <!-- 指定Spring的配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.from</url-pattern> </servlet-mapping>
步骤四:新建实体类City
代码如下所示:
package com.tarena.entity; public class City { private String id; private String name; public String getId() { return id; } public City() { super(); } public City(String id, String name) { super(); this.id = id; this.name = name; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
步骤五:新建ListController需要实现Controller接口
在该类中使用ModelMap的方式向listCity.jsp 页面传递信息,代码如下所示:
package com.tarena.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import com.tarena.entity.City; @Controller @RequestMapping("/city") public class ListController { @RequestMapping("/list.from") public String listCity(ModelMap model) { List<City> listCity=new ArrayList<City>(); listCity.add(new City("1001","北京")); listCity.add(new City("1002","上海")); listCity.add(new City("1003","深圳")); listCity.add(new City("1004","广州")); model.addAttribute("listCity", listCity); return "listCity"; } }
步骤六:修改spring-mvc.xml
在spring-mvc.xml文件中,开启@RequestMapping注解映射以及组件扫描,并且定义视图解析器ViewResolver,代码如下所示:
<context:component-scan base-package="com.tarena.controller"/> <mvc:annotation-driven/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"> </property> <property name="suffix" value=".jsp"> </property> </bean>
步骤七:新建listCity.jsp
在该页面中,使用JSTL和EL表达式向ListController.jsp传递信息,并显示在浏览器上,代码如下所示:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>城市信息列表</title> </head> <body> <table border='1' cellpadding='0' cellspacing='0' width='60%'> <caption> 城市信息列表 </caption> <tr> <td> ID </td> <td> 城市 </td> </tr> <c:forEach var="city" items="${listCity}"> <tr> <td> ${city.id} </td> <td> ${city.name} </td> </tr> </c:forEach> </table> </body> </html>
步骤八:测试
通过地址“http://localhost:8080/工程名/city/list.from”访问ListController,如图-2所示。
图 - 2
由图-2可以看出,已经成功进入了listCity.jsp页面,并显示了城市信息列表。
spring-mvc.xml文件的代码如下所示:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:component-scan base-package="com.tarena.controller"/> <mvc:annotation-driven/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"> </property> <property name="suffix" value=".jsp"> </property> </bean> </beans>
web.xml文件的完整代码如下所示:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.from</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
City类的完整代码如下所示:
package com.tarena.entity; public class City { private String id; private String name; public String getId() { return id; } public City() { super(); } public City(String id, String name) { super(); this.id = id; this.name = name; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
ListController类的完整代码如下所示:
package com.tarena.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import com.tarena.entity.City; @Controller @RequestMapping("/city") public class ListController { @RequestMapping("/list.from") public String listCity(ModelMap model) { List<City> listCity=new ArrayList<City>(); listCity.add(new City("1001","北京")); listCity.add(new City("1002","上海")); listCity.add(new City("1003","深圳")); listCity.add(new City("1004","广州")); model.addAttribute("listCity", listCity); return "listCity"; } }
listCity.jsp页面的完整代码如下所示:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>城市信息列表</title> </head> <body> <table border='1' cellpadding='0' cellspacing='0' width='60%'> <caption> 城市信息列表 </caption> <tr> <td> ID </td> <td> 城市 </td> </tr> <c:forEach var="city" items="${listCity}"> <tr> <td> ${city.id} </td> <td> ${city.name} </td> </tr> </c:forEach> </table> </body> </html>