在傳統的Spring MVC開發方法中,必須在Bean配置文件中爲每一個控制器類配置實例和請求映射和讓每一個控制器類去實現或者擴展特定於框架的接口或者基類,不夠靈活。css
若是Spring MVC能夠自動偵測你的控制器類和請求映射,就能減小配置所須要的工做量。html
Spring2.5支持一種基於註解的控制器開發方法。java
Spring能夠經過@Controller註解自動發現你的控制器類以及@RequestMapping註解中的請求映射,這樣就爲你免去了在Bean配置文件中配置它們的麻煩。此外,若是使用註解,控制器類和處理程序方法在訪問上下文資源(例如請求參數、模型屬性和會話屬性)時也會更加靈活。web
經常使用到的註解spring
一、@Controller spring-mvc
二、@RequestMappingrestful
三、@RequestParam, @PathVariable, @CookieValuecookie
@Controller註解能將任意的類標註成控制器類。與傳統的控制器相反,被標註的控制器類不須要實現特定於框架的接口,也沒必要擴展特定於框架的基類。session
在控制器類內部,可能有一個或者多個處理程序方法添加了@RequestMapping註解。mvc
處理程序方法的簽名很是靈活。你能夠爲處理程序方法指定任意的名稱,並定義如下任意一種類型做爲它的方法參數。在這裏,只提到了常見的參數類型。關於有效參數類型的完整列表,請參閱有關配置基於註解的控制器的Spring文檔。
常見的參數類型
1.HttpServletRequest、HttpServletResponse或HttpSession。
2.添加了@RequestParam註解的任意類型的請求參數
3.添加了@ModelAttribute註解的任意類型的模型屬性
4.任意類型的命令對象,供Spring綁定請求參數
5.Map或者ModelMap,供處理程序方法向模型添加屬性
6.Errors或者BindingResult,讓處理程序方法訪問命令對象的綁定和驗證結果
7.SessionStatus,讓處理程序方法發出會話處理已經完成的通知
常見的返回值類型
處理程序方法的返回類型能夠是ModelAndView、Model、Map、String、void
在建立基於註解的控制器以前,必須構建web應用程序上下文來處理註解。
首先,爲了讓Spring用@Controller註解自動偵測控制器,必須經過<context:component-scan>元素啓用Spring的組件掃描特性。
其次Spring MVC還可以根據@RequestMapping將請求映射到控制器類和處理程序方法。
爲了使其生效,必須在web應用程序上下文中註冊DefaultAnnotationHandlerMapping實例和AnnotationMethodHandlerAdapter實例。
它們分別處理在類級別和方法級別上的@RequestMapping註解。
必要的Spring MVC配置
Xml代碼
[xml] view plaincopyprint?
<?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: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/mvc/spring-mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 自動掃描註解的Controller -->
<context:component-scan base-package="com.wy.controller.annotation" />
<!-- 處理在類級別上的@RequestMapping註解-->
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!-- 處理方法級別上的@RequestMapping註解-->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 視圖解析器策略 和 視圖解析器 -->
<!-- 對JSTL提供良好的支持 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 默認的viewClass,能夠不用配置
<property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" />
-->
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
<?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: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/mvc/spring-mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 自動掃描註解的Controller --> <context:component-scan base-package="com.wy.controller.annotation" /> <!-- 處理在類級別上的@RequestMapping註解--> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <!-- 處理方法級別上的@RequestMapping註解--> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 視圖解析器策略 和 視圖解析器 --> <!-- 對JSTL提供良好的支持 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 默認的viewClass,能夠不用配置 <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" /> --> <property name="prefix" value="/WEB-INF/page/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter是默認在Web應用程序上下文中預先註冊好的。然而,若是你還顯式地註冊了其餘的處理程序映射或者處理程序適配器,它們就不會自動註冊了。在這種狀況下,你必須親自注冊它們。
基於註解的控制器類能夠是個任意類,不實現特殊接口,也不擴展特殊的基類。你只要用@Controller註解對它進行標註便可。還能夠在控制器中定義一個或者多個處理程序方法來處理單個或者多個動做。處理程序方法的簽名很靈活,足以接受一系列參數。
@RequestMapping註解能夠被應用到類級別或者方法級別上。
Controller層:代碼中寫了很詳細的註釋
Java代碼
[java] view plaincopyprint?
package com.wy.controller.annotation;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.WebRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import com.wy.pojo.User;
/**
* @author Administrator
* @version 2011-12-3
*/
@Controller
@RequestMapping("userManagerContoller") //指定請求路徑,相對路徑能夠不定義
public class UserManagerContoller {
/**
* 歡迎
* @return
*/
/* 1.傳統的獲取請求的參數方式
* http://localhost:8080/SpringMVC/userManagerContoller/welcome.do?name=wy
*/
@RequestMapping("/welcome")
public ModelAndView welcome(HttpServletRequest request){
ModelAndView mav = new ModelAndView();
String name = request.getParameter("name");
Date today = new Date();
String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(today);
mav.addObject("today", date);
mav.addObject("name", name);
mav.setViewName("welcome");
return mav;
}
/* 2.restful風格獲取請求的參數方式 Spring3.0的一個重要變化(將參數放到了請求路徑中)
* http://localhost:8080/SpringMVC/userManagerContoller/welcome/wy.do
*
* 注意點: JVM將Java文件編譯成Class文件有兩種模式 Debug 和Release
* 這兩種編譯方式的區別是:
* Debug 包含額外的調試信息,能夠完整的保留變量的名稱 (Eclipse 使用的是Debug)
* Release 把變量名稱使用其餘的一些符號代替,量名稱就不可見啦 (在使用 javac命令)
*/
@RequestMapping("/welcome/{param}/{sex}")
//前一個「param是防止Release編譯下找不到參數名稱,所以要指定;要和模板中定義的參數名稱一致
//後面一個「param」能夠和模板中定義的參數名稱不一致,建議仍是一致
public ModelAndView welcome(@PathVariable("param") String param, @PathVariable("sex") String xingbie){
ModelAndView mav = new ModelAndView();
mav.addObject("name", param);
mav.addObject("sex", xingbie);
Date today = new Date();
String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(today);
mav.addObject("today", date);
mav.setViewName("welcome");
return mav;
}
/**
* 3.不一樣請求方式(get,post),映射不一樣的方法
*
* value 指定請求路徑,method 指定請求方式
*/
@RequestMapping(value="/welcome", method=RequestMethod.GET)
public ModelAndView requestMethodGet(){
ModelAndView mav = new ModelAndView();
mav.setViewName("welcome");
return mav;
}
@RequestMapping(value="/hello", method=RequestMethod.POST)
public ModelAndView requestMethodPost(){
ModelAndView mav = new ModelAndView();
mav.setViewName("hello");
return mav;
}
/**
* 4. @RequestParam 使用方法和@PathVariable相似(要注意Debug和Release)
* http://localhost:8080/SpringMVC/userManagerContoller/welcomeParam.do?username=wy&password=123&age=23
*/
@RequestMapping(value="/welcomeParam", method=RequestMethod.GET)
public ModelAndView welcome(@RequestParam("username") String username,
@RequestParam("password") String password, @RequestParam("age") int age) {
ModelAndView mav = new ModelAndView();
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setAge(age);
mav.addObject("user", user);
mav.setViewName("hello");
return mav;
}
/**
* 5.獲取表單中的值
* BindingResult 綁定數據過程當中產生的錯誤注入到BindingResult中。
*/
@RequestMapping(value="/welcome", method=RequestMethod.POST)
public ModelAndView commonCommod(User user, BindingResult result){
ModelAndView mav = new ModelAndView();
mav.addObject(user);
if(result.hasErrors() && result.hasFieldErrors()){
String field = null;
Object fieldValue = null;
Map<String, Object> map = new HashMap<String, Object>();
List<FieldError> fieldErrors = result.getFieldErrors();
for(FieldError fieldError : fieldErrors){
field = fieldError.getField();
fieldValue = fieldError.getRejectedValue();
map.put(field, fieldValue);
}
mav.addObject("map", map);
mav.setViewName("welcome");
}else{
mav.setViewName("hello");
}
return mav;
}
/**
* 屬性編輯器 類型轉換
* 典型應用: 日期轉換
*/
@InitBinder
public void initBinder(WebRequestDataBinder binder){
binder.registerCustomEditor(Date.class, new CustomDateEditor(
new SimpleDateFormat("yyyy-MM-dd"),false));
}
public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
/**
* 6.經常使用的參數
* 使用Session有一個前提就是session必須是可用的
*/
// public ModelAndView commonArguments(HttpServletRequest request, HttpServletResponse response, HttpSession session,
// @CookieValue AnyType cookieName, @RequestHeader("user-Agent") AnyType name,
// @PathVariable AnyType variableName, @RequestParam AnyType paramName){
// ModelAndView mav = new ModelAndView();
//
// return mav;
// }
/**
* 7.返回類型
* void 、String、AnyType(任意對象)、Model、ModelAndView
* 說明:Model繼承了Map,爲SpringMVC定製的
*/
// void
@RequestMapping
public void commonReturnType(HttpServletResponse response){
try {
PrintWriter out = response.getWriter();
out.println("向頁面中輸出的值");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@RequestMapping
public void commonReturnType(PrintWriter out){//其實也是從HttpServletResponse 經過getWriter()獲得out
out.println("向頁面中輸出的值");
}
@RequestMapping("/commonReturnType")
public void commonReturnType(){
//默認生成隱含的viewName(規則測略:按照請求路徑${appName/userManagerContoller/commonReturnType.do
// ---> userManagerContoller/commonReturnType}
// ---> /WEB-INF/page/userManagerContoller/commonReturnType.jsp
// )
}
// String
/**
* ModelAndView中的Model
* ModelAndView中的View
*/
@RequestMapping
public String commonReturnType(Map<Object, Object> model){//model
model.put("", "");
return "viewName";
}
//AnyType(任意對象)
/**
* user放到model中,model(key, value) key默認是取Bean的名稱將其首字母小寫 即user,value即user
* 默認生成隱含的viewName
* 在頁面上可使用request.getAttribute("user")或者${user.key} ${user.value}
* @return
*/
@RequestMapping
public User commonReturnTypeUser(){//
User user = null;
return user;
}
/**
* userList放到model中,model(key, value) key默認是框架生成userList,value即user
* 默認生成隱含的viewName
* 在頁面上可使用request.getAttribute("userList")或者${userList.key} ${userList.value}
* @return
*/
@RequestMapping
public List<User> commonReturnTypeUserList(){
List<User> userList = null;
return userList;
}
/**
*
* 默認生成隱含的viewName
* @return
*/
@RequestMapping
public Model commonReturnTypeModel(){
Model model = null;
return model;
}
/**
*
* @return
*/
@RequestMapping
public ModelAndView commonReturnTypeModelAndView(){
ModelAndView mav = new ModelAndView();
mav.addObject("", "");
mav.setViewName("");
return mav;
}
}
package com.wy.controller.annotation; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.support.WebRequestDataBinder; import org.springframework.web.servlet.ModelAndView; import com.wy.pojo.User; /** * @author Administrator * @version 2011-12-3 */ @Controller @RequestMapping("userManagerContoller") //指定請求路徑,相對路徑能夠不定義 public class UserManagerContoller { /** * 歡迎 * @return */ /* 1.傳統的獲取請求的參數方式 * http://localhost:8080/SpringMVC/userManagerContoller/welcome.do?name=wy */ @RequestMapping("/welcome") public ModelAndView welcome(HttpServletRequest request){ ModelAndView mav = new ModelAndView(); String name = request.getParameter("name"); Date today = new Date(); String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(today); mav.addObject("today", date); mav.addObject("name", name); mav.setViewName("welcome"); return mav; } /* 2.restful風格獲取請求的參數方式 Spring3.0的一個重要變化(將參數放到了請求路徑中) * http://localhost:8080/SpringMVC/userManagerContoller/welcome/wy.do * * 注意點: JVM將Java文件編譯成Class文件有兩種模式 Debug 和Release * 這兩種編譯方式的區別是: * Debug 包含額外的調試信息,能夠完整的保留變量的名稱 (Eclipse 使用的是Debug) * Release 把變量名稱使用其餘的一些符號代替,量名稱就不可見啦 (在使用 javac命令) */ @RequestMapping("/welcome/{param}/{sex}") //前一個「param是防止Release編譯下找不到參數名稱,所以要指定;要和模板中定義的參數名稱一致 //後面一個「param」能夠和模板中定義的參數名稱不一致,建議仍是一致 public ModelAndView welcome(@PathVariable("param") String param, @PathVariable("sex") String xingbie){ ModelAndView mav = new ModelAndView(); mav.addObject("name", param); mav.addObject("sex", xingbie); Date today = new Date(); String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(today); mav.addObject("today", date); mav.setViewName("welcome"); return mav; } /** * 3.不一樣請求方式(get,post),映射不一樣的方法 * * value 指定請求路徑,method 指定請求方式 */ @RequestMapping(value="/welcome", method=RequestMethod.GET) public ModelAndView requestMethodGet(){ ModelAndView mav = new ModelAndView(); mav.setViewName("welcome"); return mav; } @RequestMapping(value="/hello", method=RequestMethod.POST) public ModelAndView requestMethodPost(){ ModelAndView mav = new ModelAndView(); mav.setViewName("hello"); return mav; } /** * 4. @RequestParam 使用方法和@PathVariable相似(要注意Debug和Release) * http://localhost:8080/SpringMVC/userManagerContoller/welcomeParam.do?username=wy&password=123&age=23 */ @RequestMapping(value="/welcomeParam", method=RequestMethod.GET) public ModelAndView welcome(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("age") int age) { ModelAndView mav = new ModelAndView(); User user = new User(); user.setUsername(username); user.setPassword(password); user.setAge(age); mav.addObject("user", user); mav.setViewName("hello"); return mav; } /** * 5.獲取表單中的值 * BindingResult 綁定數據過程當中產生的錯誤注入到BindingResult中。 */ @RequestMapping(value="/welcome", method=RequestMethod.POST) public ModelAndView commonCommod(User user, BindingResult result){ ModelAndView mav = new ModelAndView(); mav.addObject(user); if(result.hasErrors() && result.hasFieldErrors()){ String field = null; Object fieldValue = null; Map<String, Object> map = new HashMap<String, Object>(); List<FieldError> fieldErrors = result.getFieldErrors(); for(FieldError fieldError : fieldErrors){ field = fieldError.getField(); fieldValue = fieldError.getRejectedValue(); map.put(field, fieldValue); } mav.addObject("map", map); mav.setViewName("welcome"); }else{ mav.setViewName("hello"); } return mav; } /** * 屬性編輯器 類型轉換 * 典型應用: 日期轉換 */ @InitBinder public void initBinder(WebRequestDataBinder binder){ binder.registerCustomEditor(Date.class, new CustomDateEditor( new SimpleDateFormat("yyyy-MM-dd"),false)); } public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); } /** * 6.經常使用的參數 * 使用Session有一個前提就是session必須是可用的 */ // public ModelAndView commonArguments(HttpServletRequest request, HttpServletResponse response, HttpSession session, // @CookieValue AnyType cookieName, @RequestHeader("user-Agent") AnyType name, // @PathVariable AnyType variableName, @RequestParam AnyType paramName){ // ModelAndView mav = new ModelAndView(); // // return mav; // } /** * 7.返回類型 * void 、String、AnyType(任意對象)、Model、ModelAndView * 說明:Model繼承了Map,爲SpringMVC定製的 */ // void @RequestMapping public void commonReturnType(HttpServletResponse response){ try { PrintWriter out = response.getWriter(); out.println("向頁面中輸出的值"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @RequestMapping public void commonReturnType(PrintWriter out){//其實也是從HttpServletResponse 經過getWriter()獲得out out.println("向頁面中輸出的值"); } @RequestMapping("/commonReturnType") public void commonReturnType(){ //默認生成隱含的viewName(規則測略:按照請求路徑${appName/userManagerContoller/commonReturnType.do // ---> userManagerContoller/commonReturnType} // ---> /WEB-INF/page/userManagerContoller/commonReturnType.jsp // ) } // String /** * ModelAndView中的Model * ModelAndView中的View */ @RequestMapping public String commonReturnType(Map<Object, Object> model){//model model.put("", ""); return "viewName"; } //AnyType(任意對象) /** * user放到model中,model(key, value) key默認是取Bean的名稱將其首字母小寫 即user,value即user * 默認生成隱含的viewName * 在頁面上可使用request.getAttribute("user")或者${user.key} ${user.value} * @return */ @RequestMapping public User commonReturnTypeUser(){// User user = null; return user; } /** * userList放到model中,model(key, value) key默認是框架生成userList,value即user * 默認生成隱含的viewName * 在頁面上可使用request.getAttribute("userList")或者${userList.key} ${userList.value} * @return */ @RequestMapping public List<User> commonReturnTypeUserList(){ List<User> userList = null; return userList; } /** * * 默認生成隱含的viewName * @return */ @RequestMapping public Model commonReturnTypeModel(){ Model model = null; return model; } /** * * @return */ @RequestMapping public ModelAndView commonReturnTypeModelAndView(){ ModelAndView mav = new ModelAndView(); mav.addObject("", ""); mav.setViewName(""); return mav; } }
view層
welcome.jsp
Html代碼
[html] view plaincopyprint?
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<title>welcome.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
This is my annotation HTML page. <br/>
今天是: ${today}<br/>
參數是: ${name} ${sex}<br/>
<hr/>
<c:forEach var="field" items="${map}">
<c:if test="${field != null}" var="param" scope="page">
您輸入的${field.key}不正確! ${field.value}<br/>
</c:if>
</c:forEach>
<form action="<%=path%>/userManagerContoller/welcome.do" method="post">
用戶名: <input type="text" id="username" name="username" value="wy" /><br/>
密碼 : <input type="text" id="password" name="password" value="wy" /><br/>
年齡 : <input type="text" id="age" name="age" value="23wy" /><br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); %> <!DOCTYPE html> <html> <head> <title>welcome.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> This is my annotation HTML page. <br/> 今天是: ${today}<br/> 參數是: ${name} ${sex}<br/> <hr/> <c:forEach var="field" items="${map}"> <c:if test="${field != null}" var="param" scope="page"> 您輸入的${field.key}不正確! ${field.value}<br/> </c:if> </c:forEach> <form action="<%=path%>/userManagerContoller/welcome.do" method="post"> 用戶名: <input type="text" id="username" name="username" value="wy" /><br/> 密碼 : <input type="text" id="password" name="password" value="wy" /><br/> 年齡 : <input type="text" id="age" name="age" value="23wy" /><br/> <input type="submit" value="提交" /> </form> </body> </html>
值得注意的點:
一、@PathVariable("paramName") @RequestParam("paramName") 建議指定參數名稱
緣由是VM將Java文件編譯成Class文件有兩種模式 Debug 和Release
這兩種編譯方式的區別是:
Debug 包含額外的調試信息,能夠完整的保留變量的名稱 (Eclipse 使用的是Debug)
Release 把變量名稱使用其餘的一些符號代替,量名稱就不可見啦 (在使用 javac命令)
二、restful風格獲取請求的參數方式
三、參數類型轉換
註冊屬性編輯器
四、對於無任何輸出的方法
Java代碼
[java] view plaincopyprint?
@RequestMapping("/commonReturnType")
public void commonReturnType(){
//默認生成隱含的viewName(規則測略:按照請求路徑${appName/userManagerContoller/commonReturnType.do
// ---> userManagerContoller/commonReturnType}
// ---> /WEB-INF/page/userManagerContoller/commonReturnType.jsp
// )
}
@RequestMapping("/commonReturnType") public void commonReturnType(){ //默認生成隱含的viewName(規則測略:按照請求路徑${appName/userManagerContoller/commonReturnType.do // ---> userManagerContoller/commonReturnType} // ---> /WEB-INF/page/userManagerContoller/commonReturnType.jsp // ) }