【文件属性】:
文件名称:轻量级java web MVC框架
文件大小:52KB
文件格式:JAR
更新时间:2015-05-03 08:38:19
java web MVC 轻量级框架
一个非常简单的MVC框架,实现了类似Spring MVC的基本功能。
1、包括自动扫描绑定映射路径,只要在web.xml中指定扫描包,系统启动后会将请求url绑定到指定的处理方法上。如:
在web.xml中定义如下:
ScanPackage
com.mvc.controller
容器在启动时候,会将com.mvc.controller下所有映射路径绑定处理方法上,假如在扫描包中定义下列类:
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.hxw.simple.light.mvc.annotation.MappingMethodAnnotation;
import com.hxw.simple.light.mvc.view.SimpleModelView;
import com.mvc.verification.VerificationCode;
public class LoginController {
@MappingMethodAnnotation(mappingMethod = "/login.do")
public String login(HttpServletRequest request, HttpServletResponse response) {
return "login/main";
}
@MappingMethodAnnotation(mappingMethod = "/loginvm.do")
public SimpleModelView loginView(HttpServletRequest request,
HttpServletResponse response) {
SimpleModelView mv = new SimpleModelView("login/mainmv");
Map m = new HashMap();
m.put("beij", "北京");
m.put("sha", "上海");
m.put("nanj", "南京");
mv.setAttribute("city", m);
return mv;
}
@MappingMethodAnnotation(mappingMethod = "/Verify.do")
public void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws IOException {
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
response.setContentType("image/jpeg");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
VerificationCode vCode = new VerificationCode();
BufferedImage bufferImage = vCode.getImageData();
HttpSession session = request.getSession();
response.addCookie(new Cookie("JSSESIONID", session.getId()));
ServletOutputStream responseOutputStream = response.getOutputStream();
ImageIO.write(bufferImage, "JPEG", responseOutputStream);
responseOutputStream.flush();
responseOutputStream.close();
}
}
那么在接到url请求如:http://localhost:8080/TestSimpleMVC/loginvm.do
会调用指定的方法处理。
2、支持视图定义,在web.xml定义了视图路径后:
DelegateForwardServlet
com.hxw.simple.light.mvc.servlet.DelegateForwardServlet
prefix
/WEB-INF/view/
suffix
.jsp
DelegateForwardServlet
/SYS_FORWARD_URL.fo
只需在执行完方法后,返回字符串:如return "login/main"就会跳转到指定视图,还可以在视图中用EL表单式访问modelview数据,例子如:
SimpleModelView mv = new SimpleModelView("login/mainmv");
Map m = new HashMap();
m.put("beij", "北京");
m.put("sha", "上海");
m.put("nanj", "南京");
mv.setAttribute("city", m);
return mv;
3、支持数据参数自动绑定如:
@ParamAttribute(type = ParamaAttributeType.REQUEST, key = "userPassword") String s,
@ParamAttribute(type = ParamaAttributeType.SESSION, key = "11212") String s1,
@ParamAttribute(type = ParamaAttributeType.SERVLETCONTEXT, key = "32312") String s2,
@ParamAttribute(type = ParamaAttributeType.REQUEST, key = "userNames") String[] s3
系统根据参数指定的范围,这指定范围内赋值到参数上,你还可以直接使用javabean做参数绑定,如:
public class User extends PaginatedHelper {
private static final long serialVersionUID = -8225389551152428829L;
private String userName;
private String userPassword;
public User() {
super();
}
public User(String userName, String userPassword) {
super();
this.userName = userName;
this.userPassword = userPassword;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
在方法上带上 Uer user后,属性名称相同的数据就会赋值到javabean上。不必再使用繁琐的user.setUserName(request.getParameter("userName"));
3、简便的jdbc操作
有查询模板QueryTemplate,命名查询NamedQueryTemplate及bean作为参数的BeanQueryTemplate等。支持返回javabean类型,javabean列表,
MAP类型,map列表类型,统计结果queryForInt等。