1、加入jar

1545ad011767a3a55317cdff938a1748.png

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配置

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

//String name = req.getParameter("name");

System.out.println("正在运行hello方法 name:" + name);

System.out.println("User: " + u);

//req.setAttribute("msg", "hello " + name);

model.put("msg", "hello " + name);

return "hello";//逻辑名

}

/**

* \brief 定义方法hi

*

* @return

* @attention url的地方通过/home/hi的方式訪问要想訪问的地址

* @author 涂作权

* @date 2014-5-18

* @note begin modify by null

*/

@RequestMapping(value="/hi") //子路径

public String hi(){

System.out.println("正在运行hi方法");

return "hi";  //逻辑名

}

}

6、编写的hello.jsp

‘hello.jsp‘

${requestScope.msg}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐