1.创建Web项目,引入Jar包
2.在配置文件中添加注解扫描配置springmvc-config.xml,并定义视图解析器(这个代码在eclipse会报错,但是不影响程序运行)
3.编写FirstController类,在类和方法上添加相应注解
package com.itheima.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;@Controller
@RequestMapping(value = "/hello")
public class FirstController {@RequestMapping(value = "/firstController")public String handleRequest(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception{model.addAttribute("msg", "This is my second programe");return "first";}}
4.部署并启动项目,访问地址http://localhost:8080/chapter12/hello/firstController
如果页面报错,则关闭eclipse,在打开eclipse,重新运行即可。