我应该在 web.xml 中为 rest 和普通 html 创建两个不同的 servlet 条目吗 [英] should I make two different servlet entry for rest and normal html in web.xml

查看:38
本文介绍了我应该在 web.xml 中为 rest 和普通 html 创建两个不同的 servlet 条目吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 spring mvc 应用程序.

I am writing spring mvc application.

在我的应用程序中,我有网页以及其他 Web 服务来处理 ajax 调用.

In my application I have web pages as well as rest web services to handle ajax call.

我已经完成了 web.xml 中的以下条目

I have done below entry in web.xml

<servlet>
    <servlet-name>myapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring_myapp-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>myapp</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

我是否应该使用相同的 servlet 映射我的 rest url

Should I map my rest url with same servlet like

<servlet-mapping>
    <servlet-name>myapp</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

或者我应该创建新的 servlet 条目以供休息.

Or should I make new servlet entry for rest.

我已经在 pom.xml 中为org.codehaus.jackson"做了必需的条目,并且我也在 spring_myapp-servlet.xml 中做了必需的条目.

I have done required entries in pom.xml for "org.codehaus.jackson" and also I have made required entries in my spring_myapp-servlet.xml.

对于 html 页面,我在控制器中使用以下代码

For html page I am using below code in my controller

@RequestMapping(value = "/htmlUrl")
public ModelAndView ModifyValiodation(HttpServletRequest request) {
     // my code
}

我正在使用休息服务

@RequestMapping(value = "/restUrl")
public @ResponseBody Map<String, String> restUrl(HttpServletRequest request) {
     // my code
}

如果我只使用一个 servlet 进行两个 url 映射,那么总共会生成 4 个 url.

If I am using only one servlet for two url mapping, then total 4 url will be made.

  1. myapp/htmlUrl.html
  2. myapp/restUrl.html
  3. myapp/rest/htmlUrl
  4. myapp/rest/restUrl

如果我使用两个不同的 servlet 和单独的 dispacherServlet 那么我是否必须在 servlet.xml 中输入 spring 的每个组件和服务?

If I am using two different servlet with individual dispacherServlet then will i have to make entry of every component and service of spring in both the servlet.xml?

请指出暴露rest web service的解决方法.

Please point out the solution for exposing rest web service.

谢谢!

推荐答案

由您来设计基础架构的服务器端.

It is up to you to design the server side of the infrastructure.

RESTful 规范和 Servlet 规范都没有对此进行任何说明.

Neither the RESTful specifications have any instructions for doing this nor the Servlet specifications enforce anything on this.

在应用程序设计中,我认为最好保留两个不同的 servlet 来处理不同的 URL,因为随着时间的推移,这些类将变得复杂而冗长.这些可以用作前端控制器,并且可以在后端具有通用逻辑类.

On the Applications design I think it is better idea to keep two different servlets to handle different URLs because over time the classes will become complex and long. These to may be used as front controllers and may have common logic class in the backend.

这篇关于我应该在 web.xml 中为 rest 和普通 html 创建两个不同的 servlet 条目吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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