1. 程式人生 > >第八章 SpringMVC進階

第八章 SpringMVC進階

檢視解析器

<?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"
	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
	">
	
	<!-- 配置檢視解析器: 檢視解析器將返回的跳轉的頁面路徑進行加工 -->
	<!-- 檢視解析器的作用是隻對Controller類中每個函式返回值那裡的String型別前後加路徑 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
		<!-- 字首和字尾 -->
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<context:component-scan base-package="com.controller"></context:component-scan>

</beans>
package com.controller;

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

@Controller
@RequestMapping("/test")
public class TestResolver {
	@RequestMapping("/test.do")
	public String test(){
		System.out.println("檢視解析器準備執行中.........");
		
		// SpringMVC.xml配置檢視解析器;檢視解析器會自動為返回的頁面路徑新增字首和字尾
		return "index";
	}
}

SpringMVC對引數的獲取

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>index</title>
  </head>
  
  <body>
  	<form action="test/test.do" method="post">
  		帳號 : <input type="text" name="username" /><br/><br/>
  		密碼 : <input type="password" name="password" /><br/><br/>
  		<input type="submit" />
  	</form>
  </body>
</html>

             Servlet獲取引數

package com.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

/*
 * 獲取引數
 */
@Controller
@RequestMapping("/test")
public class TestResolver {
	@RequestMapping("/test.do")
	public String test(HttpServletRequest request,HttpServletResponse reponse){
		// Servlet獲取引數
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println("帳號:"+username);
		System.out.println("密碼:"+password);
		return "ok";
	}
}

             SpringMVC多形參獲取引數

package com.controller;

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

/*
 * 獲取引數
 */
@Controller
@RequestMapping("/test")
public class TestResolver {
	@RequestMapping("/test.do")
	public String test(String username,String password){
		// SpringMVC多引數獲取,形參名必須與頁面name值相同
		System.out.println("帳號:"+username);
		System.out.println("密碼:"+password);
		return "ok";
	}
}

             SpringMVC封裝物件獲取引數

package com.model;

/*
 * 	使用者封裝實體類
 */
public class User {
	private String username;
	private String password;
	public User() {
		super();
	}
	public User(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}
package com.controller;

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

import com.model.User;

/*
 * 獲取引數
 */
@Controller
@RequestMapping("/test")
public class TestResolver {
	@RequestMapping("/test.do")
	public String test(User user){
		// SpringMVC封裝物件獲取引數
		System.out.println("帳號:"+user.getUsername());
		System.out.println("密碼:"+user.getPassword());
		return "ok";
	}
}

             SpringMVC指定引數獲取

package com.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/*
 * 獲取引數
 */
@Controller
@RequestMapping("/test")
public class TestResolver {
	@RequestMapping("/test.do")
	// @RequestParam註解設定指定引數(不可變),形參名可變
	// 指定引數獲取,引數名必須與頁面name值對應,否則會報錯;其他方式不會報錯,會返回null
	public  String test(@RequestParam("username")String name,@RequestParam("password")String pwd){
		// SpringMVC註解指定引數獲取
		System.out.println("帳號:"+name);
		System.out.println("密碼:"+pwd);
		return "ok";
	}
}

ModelAndView

package com.controller;


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

/*
 * 	ModelAndView:新增資料和設定跳轉的頁面
 */
@Controller
@RequestMapping("/test")
public class TestResolver {
	@RequestMapping("/test.do")
	public ModelAndView test(String username,String password){
		
		// 建立模型檢視
		ModelAndView mav = new ModelAndView();
		// 新增模型資料:可以在頁面通過key獲取value的值
		mav.addObject("username", username);
		mav.addObject("password", password);		
		// 設定檢視:頁面會根據此進行跳轉
		mav.setViewName("ok");
		// 返回檢視模型
		return mav;
	}
}

SpringMVC寫出JSON

package com.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/*
 * 	ModelAndView
 */
@Controller
@RequestMapping("/test")
public class TestResolver {
	@RequestMapping("/test.do")
	// 返回JSON字串
	// 在方法返回型別前面新增@ResponseBody註解或在方法上面添加註解;宣告返回的是json
	public @ResponseBody void test(HttpServletResponse response) throws IOException{		
		// 獲取響應輸出流
		PrintWriter out = response.getWriter();
		// json字串
		String json="[{'name':'Micro','age',18},{'name':'Myth','age':23}]";
		// 寫出json字串
		out.write(json);		
	}
}