使用 Struts 或自定义控制器
我正在开发一个 Ajax 密集型应用程序,用户永远不会离开同一页面。然而,将会有数十种不同类型的服务器调用来获取数据,我相信这个应用程序将来会被添加到其中。对于每个调用,我需要检查以确保用户 (A) 登录 (B) 对他们的请求有足够的权限,然后用 hibernate 填充 bean,然后将其转换为 JSON、XML 或错误/登录消息。
我知道 Struts 可以做到这一点,但是当我可以制作一个概念上如下所示的自定义 servlet 控制器时,使用它有什么优势吗:
Controller extends Servlet
{
status = checkLogin()
if(status == SUCCESS)
status = checkPermissions()
if(status == SUCCESS)
status = validateCommand()
if(status == SUCCESS)
bean = factory()
if(status == LOGIN)
promptLogin()
else if(status == ERROR)
toError(status)
else if(isJson)
toJson(bean)
else
toXml(bean)
}
我觉得这可以满足我的目的,并且更简单/更小,但我绝不是 Java 专家所以我希望得到一些建议。我的首要任务是可扩展性和维护。也许我应该使用更好的框架?提前致谢!
I am developing an Ajax-heavy application, where the user never navigates away from the same page. However, there is going to dozens of different kinds of calls to the server for data and I'm confident this application will be added onto in the future. For each of these calls I'd need to check to make sure the user is (A) logged in (B) has sufficient permissions for their request, and then fill a bean with hibernate, and then convert it to either JSON, XML or an error/login message.
I know this possible with Struts, but is there any advantage to using it when I can just make a custom servlet controller that conceptually looks like this:
Controller extends Servlet
{
status = checkLogin()
if(status == SUCCESS)
status = checkPermissions()
if(status == SUCCESS)
status = validateCommand()
if(status == SUCCESS)
bean = factory()
if(status == LOGIN)
promptLogin()
else if(status == ERROR)
toError(status)
else if(isJson)
toJson(bean)
else
toXml(bean)
}
I feel this would serve my purposes and is simpler/smaller but I am no means a Java expert so I'd appreciate some advice on this. My primary priorities are scalability and maintenance. Maybe there's a better framework I should use? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Alex,使用任何框架(无论是 struts2/spring mvc)的想法就是去掉通用代码,让开发人员专注于业务逻辑,你的情况也是如此,将来正如你所提到的,它可能会变得复杂因此,最好使用任何现有框架并让它们处理请求流周期,而您可以专注于业务逻辑。
此外,当它已经被许多其他框架开发和良好测试时,重新发明轮子没有任何好处。
正如您所说,您的主要目标是可扩展性和维护,因此我相信最好使用一些众所周知的框架来实现这一点
Alex, the idea to use any framework be it struts2/spring mvc is so take away the common code and let the developer concentrate on the business logic same is the case with you and in future as you have mentioned that it can grow to complex piece so its always better to use any of the existing framework and let them to handle the request flow cycle and you concentrate on the business logic
Additional there is no benefits of re inventing the wheel when its already being developed and well tested by many others.
As you said your primary goal is scalability and maintenance so i believe that its better to use some well known framework for this
您的用例听起来已经足够复杂,您可能会从框架中受益,因为很明显您有以下要求:
为了解决您的特定问题,Struts2 可以使用 拦截器(用于安全等),Actions(S2 中 MVC 的关键)和 插件(用于您的 JSON 响应)。但是,如果您从头开始开发此应用程序,并且有一些技术余地,那么肯定还有其他可行的选择,例如 JSF(带有像 PrimeFaces)、Wicket 等。
Your use case already sounds complex enough that you might benefit from a framework, since its clear you have the following requirements:
To address your specific question, Struts2 can readily accommodate all of these requirements using Interceptors (for things like security), Actions (the crux of MVC in S2) and Plugins (for your JSON responses). However, if you're developing this application from scratch, and you have some technical leeway, there are certainly other viable options such as JSF (with a component library like PrimeFaces), Wicket and many others.