![](http://dl2.iteye.com/upload/attachment/0112/8555/3407011d-9022-3956-8ce9-eebeced064b4.png)
springmvc 模型原理
springmvc.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:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 配置Handler --> <bean id="itemsController1" name="/queryItems.action" class="com.controller.ItemsController" /> <bean id="itemsController2" class="com.controller.ItemsController2" /> <bean id="itemsController3" class="com.controller.ItemsController3" /> <!-- 配置处理器映射器 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <!-- 另一种处理器映射器 配置simple url 处理器映射器 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/queryItems1.action">itemsController1</prop> <prop key="/queryItems2.action">itemsController1</prop> <prop key="/queryItems3.action">itemsController2</prop> <prop key="/getjson.action">itemsController3</prop> </props> </property> </bean> <!-- 配置处理器适配器 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!-- 另一种处理器适配器 --> <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" /> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" /> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置前端控制器 --> <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:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
controller
package com.controller; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import com.pojo.Items; public class ItemsController implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { List<Items> itemsList = new ArrayList<Items>(); Items it1 = new Items(); it1.setName("苹果"); it1.setPrice(20.3f); it1.setDetail("很大超甜"); Items it2 = new Items(); it2.setName("香蕉"); it2.setPrice(40.3f); it2.setDetail("很大超香"); itemsList.add(it1); itemsList.add(it2); ModelAndView mv = new ModelAndView(); mv.addObject("itemsList", itemsList); mv.setViewName("/WEB-INF/pages/items/itemsList.jsp"); return mv; } }
controller2.java
package com.controller; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.HttpRequestHandler; import com.pojo.Items; public class ItemsController2 implements HttpRequestHandler{ @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Items> itemsList = new ArrayList<Items>(); Items it1 = new Items(); it1.setName("苹果"); it1.setPrice(20.3f); it1.setDetail("很大超甜"); Items it2 = new Items(); it2.setName("香蕉"); it2.setPrice(40.3f); it2.setDetail("很大超香"); itemsList.add(it1); itemsList.add(it2); request.setAttribute("itemsList",itemsList); request.getRequestDispatcher("/WEB-INF/pages/items/itemsList.jsp").forward(request, response); } }
controller3
package com.controller; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.HttpRequestHandler; import com.pojo.Items; public class ItemsController3 implements HttpRequestHandler{ @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String json="{\"name\":\"香蕉\",\"price\":\"20.0\",\"详细\":\"很好吃\"}"; response.setCharacterEncoding("utf-8"); response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json); } }
common.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <% String path1 = request.getContextPath(); String root = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path1; request.setAttribute("path1", path1); request.setAttribute("root",root); %>
itemsList.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ include file="/common/common.jsp" %> <% 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>My JSP 'itemsList.jsp' starting page</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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <table> <tr> <td>名称</td><td>价格</td><td>详情</td> </tr> <c:forEach items="${itemsList }" var="obj"> <tr> <td>${obj.name }</td> <td>${obj.price }</td> <td>${obj.detail }</td> </tr> </c:forEach> </table> </body> </html>