springMVC入门实例

项目结构:

springMVC入门实例

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>SpringMVC</display-name>

  
  <!-- 配置前端控制器 -->
  
  <servlet>
  	<servlet-name>SpringMVCServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- contextConfigLocation加载配置文件:映射器、适配器的配置 -->
  	<init-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:springMVC.xml</param-value>
  	</init-param>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>SpringMVCServlet</servlet-name>
  <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  
</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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-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/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">                            
        
        <!-- 配置Handler -->
        <bean id="ItemController1" name="/queryItem.action" class="cn.xh.ssm.controller.ItemController1"></bean>
        
        
        
        <!--********************************************************************* -->
        <!-- 处理器映射器 :将Bean的name作为Url进行查找-->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
        
        <!--  简单url映射器 :simpleUrlHandlerMapping是BeanNameUrlHandlerMapping的增强版本,它可以将url和处理器bean的id进行统一映射配置。-->
        <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/queryItem2.action">ItemController1</prop>
                    <prop key="/queryItem3.action">ItemController1</prop>
                </props>
            </property>
        </bean>
        
        
        <!--********************************************************************* -->
        <!-- 处理器映适配器-->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
        
        
        <!--********************************************************************* -->
        <!-- 视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"></property>
        
        </bean>
    
    
</beans>



item.java

package cn.xh.ssm.po;

import java.util.Date;

public class Item {

	private Long id;
	public String name;
	private Long price;
	private String detail;
	private Date createTime;
	
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Long getPrice() {
		return price;
	}
	public void setPrice(Long price) {
		this.price = price;
	}
	public String getDetail() {
		return detail;
	}
	public void setDetail(String detail) {
		this.detail = detail;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	
}




ItemController.java(相当于action)


package cn.xh.ssm.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 cn.xh.ssm.po.Item;

public class ItemController1  implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

		List<Item> itemList=new ArrayList();
		Item item1=new Item();
		item1.setName("苹果笔记本");
		item1.setPrice(6000L);
		item1.setDetail("高级货");
		
		Item item2=new Item();
		item2.setName("联想笔记本");
		item2.setPrice(5400L);
		item2.setDetail("ThinkPad T430 商务本");
		
		Item item3=new Item();
		item3.setName("苹果手机");
		item3.setPrice(5000L);
		item3.setDetail("Iphone 6 plus");
		
		itemList.add(item1);
		itemList.add(item2);
		itemList.add(item3);
		
		
		
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("itemList", itemList);
		modelAndView.setViewName("item/ItemsList");
		return modelAndView;
	}

}





List.jsp


<pre name="code" class="java"><%@ 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=UTF-8">
<title>查询商品列表</title>
</head>
<body> 
<form action="${pageContext.request.contextPath }/item/queryItem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
	<td>商品名称</td>
	<td>商品价格</td>
	<td>生产日期</td>
	<td>商品描述</td>
	<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item">
<tr>
	<td>${item.name }</td>
	<td>${item.price }</td>
	<td><fmt:formatDate value="${item.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
	<td>${item.detail }</td>
	
	<td><a href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td>

</tr>
</c:forEach>

</table>
</form>
</body>

</html>





上一篇:springMVC注解开发入门程序


下一篇:不写代码不用 Excel, 如何轻松搞定各种图形化展现