起步
Spring MVC的核心是DispatcherServlet,這個servlet充當SpringMVC的前端控制器,與其他Servlet一樣,DispatcherServlet必須在web應用程序的web.xml文件中配置
一個最簡單的web.xml配置如下,它會到springMVC-servlet.xml的文件中加載上下文
<servlet>
<!-- servlet的名字,DispatcherServlet將嘗試從一個名為springMVC-servlet.xml的文件中加載上下文 -->
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/springMVC-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 通過將DispatcherServlet映射到/,聲明了它會作為默認的servlet并且會處理所有請求,包括對靜態資源的請求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
注解
在SpringMVC的controller中常用注解有
- @Controller
- @RequestMapping
- @RequestBody
- @RequestParam
@Controller //該注解告知<context:component-scan>該類應該被自動發現并作為一個Bean注冊到Spring應用上下文
@RequestMapping(value = "/user") //根URL路徑
public class UserController {
@Autowired
UserService userService;
@RequestMapping(value = "/query_user", //表明處理"/user/query_uesr"的POST請求
method = RequestMethod.POST, //可以傳一個json支持多種方法
params = "name") //請求中必須包含參數"name",否則不處理
@ResponseBody
public String queryUser(@RequestBody QueryUserParam param, HttpServletRequest requests) {
return userService.queryUser(param, requests);
}
@ResponseBody
//RequestParam(value = "name")表明拿到http請求中的name參數賦值給name形參
public String getUserInfo(@RequestParam("name") String name, HttpServletRequest requests){
}
}
@RequestParam
默認是required的,如果沒有傳required的參數,則spring層會直接返回400。
可以使用@RequestParam(value = "name", required = false)
來使得name參數為可選。
@RequestBody
將傳入參數賦給自己定義的類,并且該類要==有一個默認的無參構造方法==,由Spring使用HttpMessageConverter將傳入的json或xml轉換成對象。
@RequestParam
和@RequestBody
的一些使用限制
request header的Content-Type | @RequestParam | @RequestBody |
---|---|---|
application/x-www-form-urlencoded | 可處理 | 可處理 |
application/json, application/xml | 不可處理 | 可處理 |
multipart/form-data | 可處理 | 不可處理 |
@RequestParam源碼
public @interface RequestParam {
/**
* The name of the request parameter to bind to.
*/
String value() default "";
/**
* Whether the parameter is required.
* <p>Default is {@code true}, leading to an exception thrown in case
* of the parameter missing in the request. Switch this to {@code false}
* if you prefer a {@code null} in case of the parameter missing.
* <p>Alternatively, provide a {@link #defaultValue() defaultValue},
* which implicitly sets this flag to {@code false}.
*/
boolean required() default true;
/**
* The default value to use as a fallback when the request parameter value
* is not provided or empty. Supplying a default value implicitly sets
* {@link #required()} to false.
*/
String defaultValue() default ValueConstants.DEFAULT_NONE;
}
路徑變量
在@RequestMapping
中使用花括號作為占位符,然后在方法中使用@PathVariable
注解路徑變量
@Controller
@RequestMapping(value = "/user")
public class UserController {
//如果url為/user/langzhouhao,那么langzhouhao將作為username傳入到getUserInfo中
@RequestMapping(value="/{username}", method=RequestMethod.get)
public String getUserInfo(@PathVariable("username") String username){
}
}
問題解決
如@Autowired
和@Value
的自動裝配必須滿足以下兩個條件:
- 自動裝配的屬性不能是static類型,因為static會隨著類加載初始化,類加載是在spring加載之前
- 外層的類必須要通過
@Component
或@Service
或@Controller
標注,才能在類內部使用自動裝配
hbase讀出的內容顯示亂碼:
controller要指定返回的編碼方式為utf-8
@RequestMapping(value = "/sutra-repo", produces = "text/html;charset=UTF-8")