SSM_config配置springmvc.xml模板

1.config配置springmvc.xml

<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.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-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/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 掃描找到了ItemsController3 -->
	<context:component-scan base-package="com.app.controller"></context:component-scan>

	<!--註解映射器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
	<!--註解適配器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

	<!-- 視圖解析器-->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置jsp路徑的前綴 -->
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<!-- 配置jsp路徑的後綴 -->
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>


2.WEB-INF配置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" 
		 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
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>springmvcfirst1001</display-name>
  
  <!-- springmvc前端控制器 -->
  <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>


3.src源碼

Controller3代碼

package com.app.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.app.po.Items;

/**
 * 
 * @Title ItemsController3.java 
 * @description TODO 
 * @time 2016年9月21日 上午11:26:34 
 * @author xxx
 * @version 1.0 
*
 */
//使用Controller標識 它是一個控制器
@Controller
public class ItemsController3 {
	
	//商品查詢列表
	//@RequestMapping實現 對queryItems方法和url進行映射,一個方法對應一個url
	//一般建議將url和方法寫成一樣
	@RequestMapping("/queryItems")
	public ModelAndView queryItems()throws Exception{
		
		//調用service查找 數據庫,查詢商品列表,這裏使用靜態數據模擬
		List<Items> itemsList = new ArrayList<Items>();
		//向list中填充靜態數據
		
		Items items_1 = new Items();
		items_1.setName("聯想筆記本");
		items_1.setPrice(6000f);
		items_1.setDetail("ThinkPad T430 聯想筆記本電腦!");
		
		Items items_2 = new Items();
		items_2.setName("蘋果手機");
		items_2.setPrice(5000f);
		items_2.setDetail("iphone6蘋果手機!");
		
		itemsList.add(items_1);
		itemsList.add(items_2);
		
		//返回ModelAndView
		ModelAndView modelAndView =  new ModelAndView();
		//相當 於request的setAttribut,在jsp頁面中通過itemsList取數據
		modelAndView.addObject("itemsList", itemsList);
		
		modelAndView.setViewName("items/itemsList");
		
		return modelAndView;
		
	}
	
}


4.模型,注意模型model和模塊module有區別

po

import java.util.Date;

public class Items {
	
    private Integer id;       // 商品ID
    private String name;      // 商品姓名
    private Float price;      // 商品價格
    private String pic;       // 商品圖片
    private Date createtime;  // 商品時間
    private String detail;    // 商品明細


5

6


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章