转载

自己写的开源MVC-easyMVC分享

简介

基本风格是按照spring mvc做的,在后期会加入一些新的特性,封装成易于自己项目使用的mvc框架。

github地址:

https://github.com/tangyanbo/easymvc

功能简介:

1. 支持将客户端参数封装成对象,类型自动转换

2. 支持注解,配置controller和方法@RequestMapping

3. 方法中可以对request和response进行访问

4. 支持jstl

5. 可配置编码

6. controller方法的参数可以任意调换

7. 404报错提醒

8. restfull支持@PathVariable

快速使用入门

jar包:

在dist目录中copy easymvc.jar和依赖jar包到web项目中

在web项目中,resources目录下建立mvc-servlet.xml

<beans>  <!-- 编码 -->  <constant name="charsetEncoding">utf-8</constant>  <!-- 扫描的目录 -->  <scan base-package="com.easymvc.controller"/> </beans> 

web.xml中配置:

<servlet>  <servlet-name>EasymvcInitServlet</servlet-name>  <display-name>EasymvcInitServlet</display-name>  <load-on-startup>1</load-on-startup>  <description></description>  <servlet-class>com.easymvc.servlet.EasymvcInitServlet</servlet-class> </servlet> <servlet-mapping>  <servlet-name>EasymvcInitServlet</servlet-name>  <url-pattern>*.html</url-pattern> </servlet-mapping> 

Controller:

@Controller //Controller注解 public class TestController {  @RequestMapping(value = "/test.html")  public String test(Model model,User user,HttpServletRequest request,HttpServletResponse response){   System.out.println("controller:"+user.getName());   List<User> list = new ArrayList<User>();   for(int i=0;i<2;i++){    User user1 = new User();    user1.setName("哈哈"+i);    list.add(user1);   }   model.addAttribute("list", list);   System.out.println(request);   System.out.println(response);   return "test";  } } 

jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <style type="text/css"> div{  padding:5px; } </style> </head> <body>  <div style="color: green">   <form action="test.html" method="POST">    <div>     name: <input name="name" type="text"/>    </div>    <div>     orderInfo.orderId: <input name="orderInfo.orderId" type="text"/>    </div>    <div>     orderInfo.user.name:<input name="orderInfo.user.name" type="text"/>    </div>    <div>     sex: <input name="sex" type="text"/>    </div>    <div>     birthDate: <input name="birthDate" type="text"/>    </div>    <div>     type: <input name="type" type="text"/>    </div>    <div>     amt: <input name="amt" type="text"/>    </div>    <div>     status: <input name="status" type="text"/>    </div>    <input type="submit">   </form>  </div> </body> </html> 

restfull支持:

@RequestMapping(value = "/test/{userId}/test3.html")     public String test3(@PathVariable("userId") String userId){         System.out.println("======================="+userId);         return "test";     }
正文到此结束
Loading...