1. 程式人生 > >SpringMVC框架基本配置

SpringMVC框架基本配置

一:springmvc框架的主要配置檔案:

1、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

2、dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- scan the package and the sub package -->
<context:component-scan base-package="com.srx.sm.demo"/>

<!-- don't handle the static resource -->
<mvc:default-servlet-handler />

<!-- if you use annotation you must configure following setting -->
<mvc:annotation-driven />

<!-- configure the InternalResourceViewResolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix"  value="/WEB-INF/views/" />
    <property name="suffix"  value=".jsp" />
</bean>

二:controller層的內容: package com.srx.sm.demo.controller;

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

import org.json.JSONArray; import org.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView;

@Controller public class ControllerDemo {

@RequestMapping(value = "welcome")
public String Demo() {
	return "first";
}

@RequestMapping(value = "getshowData_1.do")
public String showData_3(ModelMap map) {

	String message = "這個是要傳遞的資料";
	User user = new User("張三", 26, "2016-10-12");
	List<User> us = new ArrayList<User>();
	us.add(user);
	us.add(new User("李四", 27, "2016-10-13"));
	us.add(new User("王五", 28, "2016-10-14"));
	us.add(new User("趙六", 29, "2016-10-15"));

	// 將資料存入ModelMap
	map.put("message", message);
	map.addAttribute("user", user);
	map.put("users", us);
	return "show";
}

// 使用modelAndView物件將資料傳遞到前臺。
// 傳遞多個引數(不同型別的)
@RequestMapping(value = "getshowData_2.do")
public ModelAndView showData_2() {
	System.out.println("showData_2");
	String message = "這個是要傳遞的資料";
	User user = new User("張三", 12, "2016-10-12");
	List<User> us = new ArrayList<User>();
	us.add(new User("李四", 27, "2016-10-13"));
	us.add(new User("王五", 28, "2016-10-14"));
	us.add(new User("趙六", 29, "2016-10-15"));
	ModelAndView mad = new ModelAndView("angular");
	// 將資料存入modelMap
	mad.addObject("message", message);
	mad.addObject(user);// 預設為類名的首字母小寫
	mad.addObject("users", us);
	return mad;
}

@RequestMapping(value = "getshowData_3.do")
public ModelAndView showData_1() {
	String message = "123456789";
	/*
	 * 其中第一個引數為url,第二個引數為要傳遞的資料的key,第三個引數為資料物件。在這裡要注意的是:資料是預設被存放在request中的。
	 */
	return new ModelAndView("first", "information", message);
}

/**
 * @PathVariable 和 @RequestParam 的區別在於:
 * @PathVariable 的 url:getshowData_4.do/zhangsan/18/female
 * @RequestParam 的 url:getshowData_4.do?name=zhangsan&age=18&date=female
 * 
 * 				@RequestParam("name") String name, @RequestParam int
 *               age, @RequestParam("date") String date
 * 
 * @return
 */

@RequestMapping(value = "getshowData_4")
public String showData(ModelMap map) {

	// JSONObject jb = new JSONObject();
	// jb.put("name", "jack");
	// jb.put("gender", "male");
	// jb.put("age", 26);
	//
	// JSONObject jb1 = new JSONObject();
	// jb1.put("name", "lihong");
	// jb1.put("gender", "female");
	// jb1.put("age", 24);
	//
	// JSONObject jb2 = new JSONObject();
	// jb2.put("name", "bainao");
	// jb2.put("gender", "male");
	// jb2.put("age", 45);
	//
	// JSONArray ja = new JSONArray();
	// ja.put(jb);
	// ja.put(jb1);
	// ja.put(jb2);
	//
	// JSONObject j = new JSONObject();
	// j.put("data", ja);

	List<User> us = new ArrayList<User>();
	us.add(new User("jack", 27, "male"));
	us.add(new User("lihong", 28, "female"));
	us.add(new User("bainao", 29, "male"));

	// us.add(new User(name, age, date));

	map.put("json", us);

	return "jsonTest";
}

@RequestMapping("/getTemplate")
public String showTemplate(ModelMap map) {

	String message = "這個是要傳遞的資料";
	User user = new User("張三", 26, "2016-10-12");
	List<User> us = new ArrayList<User>();
	us.add(user);
	us.add(new User("李四", 27, "2016-10-13"));
	us.add(new User("王五", 28, "2016-10-14"));
	us.add(new User("趙六", 29, "2016-10-15"));

	// 將資料存入ModelMap
	map.put("message", message);
	map.addAttribute("user", user);
	map.put("users", us);
	return "show";
}

// @RequestMapping("/getshowajax") // @ResponseBody // public JSONObject showajax(ModelMap map) { // // JSONObject jb = new JSONObject(); // jb.put(“name”, “張三”); // jb.put(“gender”, “男”); // jb.put(“age”, 26); // return jb; // }

@RequestMapping("/ajax")
@ResponseBody // 處理 AJAX請求,返回響應的內容,而不是 View Name
public String handleAjax() {
	return "{username: \"zhangsan\", password:\"123456\"}";
}

} 三:SpringMVCDemo/WebContent/WEB-INF在該目錄下新建showData.jsp檔案

<%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%> <%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>

獲取資料 request:${requestScope.message} session:${sessionScope.message} application:${applicationScope.message}

ModelMap中的資料

${requestScope.message} 姓名:${requestScope.user.name} 年齡:${requestScope.user.age} 日期時間:${requestScope.user.date} 輸出的集合物件:${users}
<p>列表</p>

<c:forEach items="${users}" var="a">
	<%-- ${a.name}-${a.age}-${a.birth}<br/> --%>
	<table width="300px" height="30px" border="1" cellspacing="0" bordercolor="black">
		<tr align=center>
			<td><c:out value="${a.name}" /></td>
			<td><c:out value="${a.age}" /></td>
			<td><c:out value="${a.date}" /></td>
		</tr>
	</table>

</c:forEach>