熬夜手写个SpringMVC框架( 三 )

7.web.xml 配置:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><display-name>Archetype Created Web Application</display-name><!--启动监听器配置--><listener><listener-class>com.kexun.mvc.listen.InitMapping</listener-class></listener><!--统一拦截Servlet配置--><servlet><servlet-name>webServlet</servlet-name><servlet-class>com.kexun.mvc.servlet.WebServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><!--静态资源默认处理Servlet--><servlet><servlet-name>default</servlet-name><servlet-class>org.Apache.catalina.servlets.DefaultServlet</servlet-class><init-param><param-name>debug</param-name><param-value>0</param-value></init-param><init-param><param-name>listings</param-name><param-value>false</param-value></init-param><load-on-startup>0</load-on-startup></servlet><!--静态资源默认处理Servlet--><servlet-mapping><servlet-name>default</servlet-name><url-pattern>/static/*</url-pattern></servlet-mapping><!--统一拦截servlet 拦截所有请求--><servlet-mapping><servlet-name>webServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>8.项目所用到的类库
 
<dependencies><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency><dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.9.3</version></dependency><dependency><groupId>MySQL</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.73</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>org.apache.Tomcat</groupId><artifactId>tomcat-catalina</artifactId><version>8.5.61</version></dependency></dependencies> 
9.基本使用
使用方法基本与SpringMvc一致 , 注解大同小异。
示例代码如下:
public class IndexController {//获取字符串 , int类型参数 返回页面@RequestMapping("index")public String index(HttpServletRequest request, String username, Integer age) throws Exception {request.setAttribute("username", username);request.setAttribute("age", age);return "index";}//获取对象参数 返回对象@ResponseBody@RequestMapping("addManage")public Map<String, Object> addManage(Manage manage) throws Exception {System.out.println("manage:" + manage);Map<String, Object> result = new HashMap<>();result.put("code", 0);result.put("message", "添加成功");return result;}//获取JSON请求 解析为对象 返回对象类型@ResponseBody@RequestMapping("addManageJSON")public Map<String, Object> addManageJSON(@RequestBody Manage manage) throws Exception {System.out.println("manage:" + manage);Map<String, Object> result = new HashMap<>();result.put("code", 0);result.put("message", "添加成功");return result;}} 
此框架实用与一些小项目 , 案例 , 需要快速搭建的 , 开箱即用的场景;没有复杂的配置 , 同时也适用于学习 , 不允许适用开源框架的场景下 , 这便是个很好的选择
项目源码已上传至码云:
https://gitee.com/gdianqimeng/kexun-mvc-orm
个人博客地址:https://muzidong.com




推荐阅读