Java On Rails?

对于类似于 Ruby On Rails的URI影射方式,Java Web开发也可以实现。参考 http://code.google.com/p/jmap/

假定有这样一个URI: /jmap/main/login,这里web context path是"/jmap",servlet mapping是" /main/*",这样的URI会被一个有预定包前缀的类package.prefixname.controller.Main的Login方法所响应。大概有以下的样例代码:

package com.lvup.webnav.test.controller;
...
public class Main extends ControllerBase {
...
    @CreateBean(beanClassName="com.lvup.webnav.test.bean.main.Login", requestAttrName="MainLogin")
    public void Login() {
        try {
            // If you use @CreateBean(false), you have to write 2 line code below.
            // Login login = (Login) createBean();
            // this.getRequest().setAttribute("MainLogin", login);
            // this request scope object may be used in jsp for data display.            
            Login login = (Login) getRequest().getAttribute("MainLogin");

            // the "p" is a HashMap for jsp file include
            this.p.put("pageContent", "/mains/login.jsp");
            this.p.put("title", "Login " + getRequest().getMethod()
            + " urlHint is " + this.getUrlHint());

            // the method "render" takes a string like "/mains/index.jsp"
            // in this case, INDEX_JSP is a final static string in class "Main"
            // you can refer to the /mains/index.jsp to understand the usage of HashMap "p"
            this.render(INDEX_JSP);
        } catch(Exception ex) {
            ex.printStackTrace();
        }    
    }
...
}
web context path是会随着发布的不同而变化的,所以不能作为影射规则的一部分。如果有另一个servlet mapping "/product/*",则URI: /jmap/product/list/category1会被影射到package.prefixname.controller.Product类的List方法,URI中的"category1"会作为类成员数据urlHint传递给Main类。
目前,该框架还提供了基于annotation的form数据影射和验证方法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值