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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值