springmvc是spring公司对于mvc模式中web层的支持框架,对于一些配置,要牢牢记住.
首先要配置web.xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value> classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
其次要配置springmvc.xml.里面需要的操作不多,引入约束
在写两句。
<context:component-scan base-package="cn.fzj.crotroller">
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
然后需要做的是写我们自定义的处理器handler也叫cortroller,使用@Crotroller注解类,使用RequestMapping()注解对请求的处理方法
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ACrotroller {
@RequestMapping("/gg")
public ModelAndView t() {
ModelAndView mav = new ModelAndView();
mav.addObject("zxc","大头鸭");
mav.setViewName("/WEB-INF/jsp/hello.jsp");
return mav;
}
}
然后在写我们的视图即可。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
你好
${zxc}
</body>
</html>
通过浏览器访问即可!!