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));