第八章.Spring MVC

基于MyEclipse开发

 

工程结构:

 

所使用到的jar:

 

 

代码:

FruitControllerTest.java

public class FruitControllerTest implements Controller {
        @Override
        public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
         
            List<Fruits> fruitsList = FruitsService.queryFruitsList();
            System.out.println("---");
            System.out.println(fruitsList);
            ModelAndView modelandView = new ModelAndView();
            
            modelandView.addObject("fruitsList", fruitsList);
            
            modelandView.setViewName("/WEB-INF/jsp/fruits/fruit.jsp");
            return modelandView;
        }
}

class FruitsService { public static List<Fruits> queryFruitsList(){ List<Fruits> fruitsList = new ArrayList<Fruits>(); Fruits apple = new Fruits(); apple.setName("红富士苹果"); apple.setPrice(1.5); apple.setProducing_area("安徽"); Fruits Banana = new Fruits(); Banana.setName("香蕉"); Banana.setPrice(2.9); Banana.setProducing_area("上海"); fruitsList.add(apple); fruitsList.add(Banana); return fruitsList; } }

 Fruits.java

public class Fruits {

    private String name;
    private double price;
    private String producing_area;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getProducing_area() {
        return producing_area;
    }
    public void setProducing_area(String producing_area) {
        this.producing_area = producing_area;
    }
}

 

 

 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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
<bean name="/query_test.action" class="cn.com.mvc.controller.FruitControllerTest"></bean> </beans>

 

 

 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

  

 fruit.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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 'fruit.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">
  </head>
  <body>
        <h3>新鲜水果</h3>
        <table width="300px" border="1">
            <tr>
                <td>名称</td>
                <td>价格</td>
                <td>产地</td>
            </tr>
            <c:forEach items="${fruitsList}" var="fruit">
                <tr>
                    <td>${fruit.name}</td>
                    <td>${fruit.price}</td>
                    <td>${fruit.producing_area}</td>
                </tr>
            </c:forEach>
        </table>
        
        <h4>hello</h4>
    
  </body>
</html>

 

 

 输入地址:

http://localhost:8080/SpringMVC_1/query_test.action

得到下图:

 

posted @ 2018-10-20 14:04  MrChengs  阅读(359)  评论(0编辑  收藏  举报