1 添加spring mvc所需的jar包.
2 建立一個以下項目結構的springmvc項目
3 web.xml的配置如下:
<?xmlversion="1.0"encoding="utf-8"?>
<web-appversion="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
</servlet>
<servlet-mapping>
<!--structs用/*,springmvc不能是/*類型,要是*.xxx類型
-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
4 springmvc-servlet.xml的配置,這裡的springmvc-servlet.xml名字預設是web.xml檔案中的“映射名稱”+“-”+“servlet”+”.xml”檔案。
<beansxmlns="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"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
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
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!--
如果不配置映射處理器會采用預設的映射處理器beannamehandlermapping
下面可以通過:http://localhost:8080/springmvc/hello.do來通路
-->
<beanid="testcontroller"name="/hello.do"class="com.rl.controller.testcontroller"/>
<!--定義視圖解析器
<bean
class="org.springframework.web.servlet.view.internalresourceviewresolver">
<propertyname="prefix"value="/web-inf/jsp/"></property>
<propertyname="suffix"value=".jsp"></property>
</bean>
<beanclass="org.springframework.web.servlet.handler.simpleurlhandlermapping">
<propertyname="mappings">
<props>
<!--通過:http://localhost:8080/springmvc/hello1.do可以通路
<propkey="/hello1.do">testcontroller</prop>
<!--通過:http://localhost:8080/springmvc/toindexbean.do可以通路
<propkey="/toindexbean.do">toindexbean</prop>
</props>
</property>
class="org.springframework.web.servlet.handler.beannameurlhandlermapping"></bean>
<!--使用controller的類名的首字元小寫加.do
class="org.springframework.web.servlet.mvc.support.controllerclassnamehandlermapping"></bean>
<beanid="toindexbean"name="/toindex.do"
class="org.springframework.web.servlet.mvc.parameterizableviewcontroller">
<!--也需要使用視圖解析器來解析
<propertyname="viewname"value="jsp1/index"></property>
<!--通過:http://localhost:8080/springmvc/toindex1.do通路
<beanname="/toindex1.do"
<!--
也需要使用視圖解析器來解析,下面的實際通路的是位址是:
上面的prefix
+下面的value +上面的suffix
-->
通路方式是:http://localhost:8080/springmvc/comm.do?id=2&name=toto&xxxxx=方式
在控制台可以看到輸出結果
<beanname="/comm.do"class="com.rl.controller.commcontroller">
<!--指定controller中的object類型的參數上的資料類型
<propertyname="commandclass"value="com.rl.model.person"></property>
這種可以做簡單的類型轉換,如果資料類型不一緻,它會傳回到頁面中
通路路徑是:http://localhost:8080/springmvc/form.do-->
<beanname="/form.do"class="com.rl.controller.formcontroller">
<!--指定controller中object類型的參數上的資料類型
<propertyname="formview"value="form"></property>
<propertyname="successview"value="success"></property>
</beans>
5 testcontroller的代碼如下:
package com.rl.controller;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.springframework.web.servlet.modelandview;
import org.springframework.web.servlet.mvc.abstractcontroller;
/**
* @brief testcontroller.java springmvc中controller層
* @attention
* @author toto
* @date 2014-12-28
* @note begin modify by塗作權
*/
public class testcontroller extends abstractcontroller {
@override
protected modelandview handlerequestinternal(httpservletrequest arg0,
httpservletresponse arg1) throws exception {
system.out.println("hello springmvc");
//視圖解析器在解析modelandview的時候會自動追加字首和字尾,
//如果視圖解析器的字首下依然有目錄結構,在modelandview中
//來指定字首和字尾直接的一段路徑
return new modelandview("jsp1/index");
}
}
浏覽器通路位址:http://localhost:8080/springmvc/hello.do結果如下:
浏覽器通路位址:http://localhost:8080/springmvc/hello1.do結果如下:
6 person的javabean代碼如下:
package com.rl.model;
import java.sql.date;
*@brief
person.java person對應的javabean
*@attention
*@authortoto-pc
*@date
2014-12-28
*@note
begin modify by塗作權
publicclass
person {
integer
id;
string
name;
age;
address;
/**
*
修改所有同名的變量shift +alt + r
*/
date
birthday;
@return the id
public
integer getid() {
returnid;
}
@param id the id to set
publicvoid
setid(integer id) {
this.id
= id;
@return the name
string getname() {
returnname;
@param name the name to set
setname(string name) {
this.name
= name;
@return the age
integer getage() {
returnage;
@param age the age to set
setage(integer age) {
this.age
= age;
@return the address
string getaddress() {
returnaddress;
@param address the address to set
setaddress(string address) {
this.address
= address;
@return the birthday
date getbirthday() {
returnbirthday;
@param birthday the birthday to set
setbirthday(date birthday) {
this.birthday
= birthday;
7 commcontroller的代碼如下:
import org.springframework.validation.bindexception;
import org.springframework.web.servlet.mvc.abstractcommandcontroller;
import com.rl.model.person;
* @brief commcontroller.java
@suppresswarnings("deprecation")
public class commcontroller extends abstractcommandcontroller {
/**
* object的參數用來接受實體參數,person中的屬性的接收傳
*
遞參數的名字一定要和person中的set方法後名的字元串比對,
大小寫不區分
*/
protected modelandview handle(httpservletrequest arg0,
httpservletresponse arg1, object obj, bindexception arg3)
throws exception {
person p = (person) obj;
system.out.println("id:" + p.getid() + " name:" + p.getname());
return new modelandview("index");
浏覽器中的位址:http://localhost:8080/springmvc/comm.do?id=2&name=toto結果如下:
控制台中的内容如下:
8 formcontroller的代碼如下:
import java.text.simpledateformat;
import org.springframework.beans.propertyeditors.customdateeditor;
import org.springframework.web.bind.servletrequestdatabinder;
import org.springframework.web.servlet.mvc.simpleformcontroller;
public class formcontroller extends simpleformcontroller {
protected void dosubmitaction(object command) throws exception {
system.out.println(command);
注冊時間類型的屬性轉換器
protected void initbinder(httpservletrequest request,
servletrequestdatabinder binder) throws exception {
binder.registercustomeditor(date.class,
new customdateeditor(new simpledateformat("yyyy-mm-dd"), true));