springMVC參數傳遞(一)

第一步:web.xml文件是一個工程必不可少的配置文件。javascript

<!-- lang: 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>springmvc-chapter2</display-name>
<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>

<!-- 配置Servlet的分發以及對springMVC文件的初始路徑的引用 -->
<servlet>
	<servlet-name>chapter1</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
		    <param-name>contextConfigLocation</param-name>
		    <param-value>classpath*:config/chapter1-servlet.xml</param-value>
		</init-param>
	
	<!-- 標識啓動容器時初始化該Servlet -->
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>chapter1</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 配置系統的編碼集合 -->
<filter>
	<filter-name>CharacterEncodingFilter</filter-name>
	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
	<init-param>
		<param-name>encoding</param-name>
		<param-value>utf-8</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>CharacterEncodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>html

第二步:springMVC的配置文件,chapter1-servlet.xmljava

<!-- lang: 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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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 
        			http://www.springframework.org/schema/mvc
        			http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	
	<!-- 開啓註解,由於每次啓動都會調用下面兩個類,因此springMVC將其集成到一個配置裏面,配置了
	這個,下面兩個啓用註解的類就能夠不配置了 -->
	<mvc:annotation-driven />
	
	<!-- 此配置的做用是用來掃描全部須要註解的類,只有被掃描的類才能使用註解 -->
	<context:component-scan base-package="cn.javass.chapter2.web.controller"/>
	
	<!-- 啓用註解,這兩個類是使用註解時必須所配置的類,第一個做用是經過url找到類,第二個做用是經過類找到方法-->
  	<!-- 	<bean
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
			 <bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> -->
	
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>web

第三步:定義一個表單提交的jsp頁面,hello.jsp和一個處理後的跳轉jsp頁面,mutil.jspspring

<!-- lang: html -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>Hello World</title>
</head>
<script type="text/javascript">
function add(){
	//獲取表單
	var form = document.getElementById("addForm");
	//設置請求方式爲post,若是是get的話就要對發送過去的參數先進行編碼而後再解碼,此處不做演示,不然會亂碼
	form.method="post";
	//指定訪問路徑
	form.action = "add";
	//提交表單
	form.submit();
}
</script>

<body>
<form id="addForm">
	姓名:<input type="text" name="userName"/>
	年齡:<input type="text" name="age"/><br/>
	<input type="button" value="提交" onclick="add()"/>
</form>

</body>
</html>

mutil.jspspring-mvc

<!-- lang: html -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>Hello World</title>
</head>
<body>
姓名:${userName }<br/>
年齡:${age }
</body>
</html>

第四步:創建一個Controller,MutilController.javatomcat

<!-- lang: java -->
package cn.javass.chapter2.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
@RequestMapping("/test")
public class MutilController extends MultiActionController {

/**
 * form表單提交後進入此方法進行處理
 * @param request
 * @param response
 * @param userName form表單中提交的元素userName
 * @param age form表單中提交的元素age
 * @return  mutil.jsp頁面
 */
@RequestMapping("/add")
public String add(HttpServletRequest request,HttpServletResponse response,String userName,String age){
	//jsp頁面form表單中的元素,經過方法的形參傳遞進來,這是本次參數傳遞的核心
    System.out.println("姓名:======"+userName);
	System.out.println("年齡:======"+age);
	//將參數設置到request會話中,給jsp頁面獲取
	request.setAttribute("userName", userName);
	request.setAttribute("age", age);
	return "/mutil";
}

/**
 * 首先頁面請求進入hello.jsp頁面
 * @param request
 * @param response
 * @return
 */
@RequestMapping("/hello")
public String test(HttpServletRequest request,HttpServletResponse response){
	return "/hello";
    }
}

啓動tomcat,訪問http://localhost:8080/springmvc-chapter2/test/hello,表單提交頁面hello.jsp 在此輸入圖片描述mvc

點擊提交,跳轉到mutil.jsp頁面,控制檯打印信息和jsp頁面以下圖 在此輸入圖片描述 在此輸入圖片描述app

至此,一個最基本的springMVC頁面和Controller的參數傳遞就實現了!可是,這種方法不多用,由於表單元素少還好,一旦不少,全部的參數都看成形參傳遞進來,就不符合面向對象的思想,所以不合適。jsp

相關文章
相關標籤/搜索