English 中文(简体)
以 HttpServlet 请求为网页服务的最佳模式是什么?
原标题:what is a good pattern for serving a web page given a HttpServletRequest?

我创建了一个抽象的基础类 < code> page , 以计算如何构建动态网页。 我试图找到一个好的方法, 以生成一个基于 GET 请求的 < code> page 。 例如,...

public class RootServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {

        Page page = Page.generatePage(request);

        // do stuff with page and write back response
    }
}

generatePage () 方法中,我必须不知怎么弄清楚需要哪个页面, 构建正确的页面, 然后返回一个实例。 但我不确定该如何做得很好... 例如, 我需要处理这些 URL :

http://example.com/       : build the default home page
http://example.com/ab123  : build the page corresponding to the given token "ab123"
http://example.com/about/ : build the "about" page
http://help.example.com/  : build the "help" page

这些“页面”中的每一页都扩展了抽象的基础等级Page ,让他们知道如何建立自己,但我不确定如何确定需要建造 AboutPage ,或HelpPage ,而不是默认的HomePage

我使用""http://pastread.apache.org/"rel="nofollow" >Apache Evercity 作为模板引擎,所以这些 Page 对象实际上只包含生成该页面所需的重要信息,例如要使用的样式和脚本,以及在页面上显示的相关内容。

我认为这样做比看URL的结尾更好,看看“关于”是否是一个用于构建 AboutPage 的子字符串。例如,有什么建议吗?

最佳回答

有很多架子外的工具框架可以帮助你。至少我建议春季MVC能快速工作。

问题回答

Spring MVC 利用带有附加说明的方法的控制器 来处理这种事情 来处理你想要的具体模式

他们在这里应用了一个很好的例子:

https://github.com/SpringSources/spring-mvc-showcase>https://github.com/SpringSources/spring-mvc-showcase

无论如何,用java代码建立网页不是好做法。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签