java Web应用程序中的动态URL(如在rails中) [英] dynamic URLs in java web application (like in rails)

查看:269
本文介绍了java Web应用程序中的动态URL(如在rails中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名Ruby on Rails开发人员,用Java编写Web应用程序。我试图实现类似于Rails中实现的东西。在Rails中,当Users是Model并且1是特定用户的id时,可以使用 localhost:8000 \Users\1 调用链接。我想在Java中得到同样的东西。

I'm a Ruby on Rails developer programming a web application in Java. I am trying to achieve something similar to what is achieved in Rails. In Rails it is possible to call a link using localhost:8000\Users\1 when Users is a Model and 1 is the id of a specific user. I would like to get the same kind of thing in Java.

我在MVC类型设计中工作,我的JSP页面是视图,我的Servlet是控制器。我创建了一个名为 Users 的servlet,它现在呈现 users.jsp 页面,我可以使用URL <到达该页面code> localhost:8000 \ projectName \Users ,我想路由 localhost:8000 \ projectName \Users\1 到页面 user.jsp ,而相应的Servlet将处理向页面发送正确用户(id = 1)。

I am working in an MVC type design where my JSP pages are the view and my Servlets are the controllers. I created a servlet called Users which renders the users.jsp page now i can get to that page using the URL localhost:8000\projectName\Users, i would like to route localhost:8000\projectName\Users\1 to the page user.jsp while the appropriate Servlet will handle sending into the page the correct user (with id=1).

我知道如何实现这个目标吗?

Any idea how I can achieve this?

我在大学项目中这样做,不允许使用任何框架。我也宁愿我可以编码而不是安装。

I'm doing this in a University project and am not allowed to use any frameworks. I also would rather something i could code rather than install.

推荐答案


现在我可以使用URL localhost:8000 \ projectName \ Users访问该页面,我想将localhost:8000 \ projectName \Users\1路由到页面user.jsp,而相应的Servlet将处理发送到页面正确的用户(id = 1)。

简单。将servlet映射到 / Users / * 的URL模式,而不是 / Users 。然后,您可以在URL中获取路径信息( <$ em> <$ em $ c> / Users 之后的部分,因此 / 1 在你的例子中)如下:

Simple. Map the servlet on an URL pattern of /Users/* instead of /Users. You can then grab the path info (the part after /Users in the URL, which is thus /1 in your example) as follows:

String pathInfo = request.getPathInfo();
// ...

您可以转发到用户.jsp 通常的方式。

You can just forward to users.jsp the usual way.

Long id = Long.valueOf(pathInfo.substring(1));
User user = userService.find(id);
request.setAttribute("user", user);
request.getRequestDispatcher("/WEB-INF/users.jsp").forward(request, response);

这篇关于java Web应用程序中的动态URL(如在rails中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆