配置pom.xml文件
4.0.0
com.monkey1024
02mvc
0.0.1-SNAPSHOT
war
junit
junit
3.8.1
test
javax.servlet
javax.servlet-api
3.1.0
org.springframework
spring-webmvc
5.0.4.RELEASE
02mvc
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
UTF-8
配置web.xml
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springMVC
/
配置springmvc.xml文件
Controller
package com.monkey1024.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/*
* 使用注解后就不需要实现接口了
*/
@Controller//表示当前类是一个controller
@RequestMapping("/test")//表示是一个命名空间namespace,抽取出重复的部分
public class HelloSpringMVC {
// 需要处理的url
// @RequestMapping("/test/test2.do")
@RequestMapping("/test2.do")
// 方法名可以随意命名
public ModelAndView test1(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
// 类似键值对
mv.addObject("hello","test2");
// 连接到jsp
mv.setViewName("first");
return mv;
}
/*
* 以数组的形式,下面两个都可以调用这个方法
*/
// @RequestMapping({"/test/hello.do","/test/world.do"})
@RequestMapping({"/hello.do","/world.do"})
public ModelAndView test2(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
// 类似键值对
mv.addObject("hello","helloworld");
// 连接到jsp
mv.setViewName("first");
return mv;
}
}
jsp页面
Insert title here
${hello}