spring mvc常用知識點總結


1.spring mvc是靠spring 啟動的。通過springjar包的org.springframework.web.servlet.DispatcherServlet這個servlet類具體啟動的。
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

這個dispatcherservlet這個類,有個contextConfigLocation參數,指定springmvc的配置文件xml。
2.理清一下思路:每個tomcat下的程序war包都有一個web.xml。web.xml可以啟動servlet。
web.xml只能配置servlet相關信息,如servletmapping等等,不能配置bean的注入,掃描包路徑,注解驅動等,這

3.二、配置解析

  1.Dispatcherservlet

  DispatcherServlet是前置控制器,配置在web.xml文件中的。攔截匹配的請求,Servlet攔截匹配規則要自已定義,把攔截下來的請求,依據相應的規則分發到目標Controller來處理,是配置spring MVC的第一步。
4. <!-- scan the package and the sub package -->
<context:component-scan base-package="test.SpringMVC"/>

<!-- 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 class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前綴 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后綴 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>

5.spring mvc配置文件里的配置標簽,必背:
<context:component-scan base-package="test.SpringMVC"/>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"

<!-- scan the package and the sub package -->
<context:component-scan base-package="test.SpringMVC"/>

<!-- 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 class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前綴 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后綴 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>

6.spring mvc的那些controller標簽,是注入到spring的ioc容器中的。不是spring mvc自己的ioc,spring mvc沒有自己的ioc容器,都是spring的

7.@RequestBody

  該注解用於讀取Request請求的body部分數據,使用系統默認配置的HttpMessageConverter進行解析,然后把相應的數據綁定到要返回的對象上 ,再把HttpMessageConverter返回的對象數據綁定到 controller中方法的參數上

  @ResponseBody

  該注解用於將Controller的方法返回的對象,通過適當的HttpMessageConverter轉換為指定格式后,寫入到Response對象的body數據區

8.requestParam正確用法@RequestParam(value = "invitCode", required = false) string invitCode
requestParam修飾的東西全在小括號里面()。

9.四、自動匹配參數
//match automatically
@RequestMapping("/person")
public String toPerson(String name,double age){
System.out.println(name+" "+age);
return "hello";
}
 五、自動裝箱

  1.編寫一個Person實體類
package test.SpringMVC.model;

public class Person {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private int age;

}

  2.在Controller里編寫方法

//boxing automatically
@RequestMapping("/person1")
public String toPerson(Person p){
System.out.println(p.getName()+" "+p.getAge());
return "hello";
}


10.在SpringMVC中,bean中定義了Date,double等類型,如果沒有做任何處理的話,日期以及double都無法綁定。


解決的辦法就是使用spring mvc提供的@InitBinder標簽

11.比較簡單的可以直接應用springMVC的注解@initbinder和spring自帶的WebDataBinder類和操作

[java] view plain copy
在CODE上查看代碼片派生到我的代碼片

@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

還要在springMVC配置文件中加上

[html] view plain copy
在CODE上查看代碼片派生到我的代碼片

<!-- 解析器注冊 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessageConverter"/>
</list>
</property>
</bean>
<!-- String類型解析器,允許直接返回String類型的消息 -->
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>

這樣就可以直接將上傳的日期時間字符串綁定為日期類型的數據了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM