Spring MVC 简单案例

来源:互联网 发布:2016淘宝店铺夫妻转让 编辑:程序博客网 时间:2024/06/11 10:51

     刚学习SpringMVC,写了个学习的小例子,给刚学习框架的菜鸟参考哦!下面是我的目录:

下面是web.xml里面的内容,配置了前端控制器:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>Springmvc01</display-name>    <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.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>

接下来是框架的配置文件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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    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.0.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.0.xsd    http://www.springframework.org/schema/mvc      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><bean name="/hello.action" class="com.zmj.springmvc.controller.Demo01"/> <!-- HandlerMapping -->  <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>     <!-- HandlerAdapter -->  <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>  <!-- ViewResolver --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>  </beans>

下面是实体类Items.java:

public class Items {private String name;private Double price;private String detail;public Items() {// TODO Auto-generated constructor stub}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 getDetail() {return detail;}public void setDetail(String detail) {this.detail = detail;}}

下面是controller,或者叫handler,实质就是一servlet:

public class Demo01 implements Controller {


 @Override
 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
  
        List<Items> ilist = new ArrayList<Items>();
        Items item = new Items();
                  item.setName("zou");
                  item.setPrice(43434d);
                  item.setDetail("detail");
                  ilist.add(item);
    
        ModelAndView mv = new ModelAndView(); 
        //添加模型数据 可以是任意的POJO对象 
        mv.addObject("array", ilist); 
        //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面 
        mv.setViewName("/WEB-INF/jsp/list.jsp"); 
        return mv; 
 }

}

下面是list.jsp页面代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"    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" %><% %><!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>商品列表</title></head><body><table border="solid"><tr><td>商品名称</td><td>商品价格</td><td>商品描述</td></tr><c:forEach items="${array}" var="item"><tr><td>${item.name }</td><td>${item.price }</td><td>${item.detail }</td></tr></c:forEach></table></body></html>

代码就是这些了,下面是运行效果图,访问地址是hello.action,下面是效果图:


就是这些了,慢慢的进步!吐舌头


1 0