基于注解的Spring MVC(一)

2014-11-23 21:25:39 · 作者: · 浏览: 26

1、添加jar

\

2、web.xml配置:


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_2_5.xsd">

action
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:springMVC.xml



action
/



index. jsp


3、springMVC.xml配置


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:m vc="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.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 ">










4、实体bean

package cn.itcast.springmvc.domain;

public class User {

private String name;
private String address;
private Integer age;
private String tel;

public String getName() {
return name;
}

public void setName(String name) {
System.out.println("正在通过setName方法注入name的值:" + name);
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getTel() {
return tel;
}

public void setTel(String tel) {
this.tel = tel;
}

@Override
public String toString() {
return "{name:" + name + ",address:" + address + ",age:" + age
+ ",tel:" + tel + "}";
}

}

5、编写HomeController,代码如下:

package cn.itcast.springmvc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.itcast.springmvc.domain.User;

/**
* @brief IAccountDao.java 学习Spring注解方式
* @attention
* @author 涂作权
* @date 2014-5-18
* @note begin modify null
*/
@Controller //添加注解
@RequestMapping(value = "/home") // 根路径,有些类似strut2的命名空间
public class HomeController {

/**
* 子路径,表示只支持get提交
* @param req 可以通过传递HttpServletRequest的方式获得参数
* @param name 表示连接的地方有:XXX name=
* @param u 如果url的?后面参数过多,要想获得参数,可以直接将这个参数写成User
* @param model :定义一个Map对象,可以通过这种方式将之传递给jsp页面
*
* @attention url地址可以是:http://localhost:8081/SpringMVC_02/home/hello
* name=toto&address=haidian&age=24&tel=136XXX
* 获得的参数为:正在执行hello方法 name:toto User: {name:toto,address:haidian,age:24,tel:136XXX}
* @return
*/
@RequestMapping(value="/hello",method=RequestMethod.GET)
public String hello(HttpServletRequest req,
@RequestParam(value = "name")
String name, User u, Map model) {