简体   繁体   English

在 sling servlet 中调用除 doGet 或 doPost 之外的不同方法

[英]Call different method apart from doGet or doPost in sling servlet

I am new to Sling framework.我是 Sling 框架的新手。 Is there any way we can call different methods.有什么方法可以调用不同的方法。

For ex - on Page I have add,delete, edit button so apart from creating three files i can create three methods(add, delete,update) in same file.对于前 - 在页面上,我有添加、删除、编辑按钮,所以除了创建三个文件之外,我还可以在同一个文件中创建三种方法(添加、删除、更新)。

Please suggest.请建议。

I tried by changing the method name我尝试通过更改方法名称

@Property(name = "sling.servlet.methods", value = { "getData" })

But it is not working但它不工作

@Service
@Properties({
    @Property(name = "sling.servlet.paths", value = { "getData" }),
    @Property(name = "sling.servlet.methods", value = { "GET" })
     })
public class getData extends SlingAllMethodsServlet {

The SlingAllMethodsServlet will support any of the valid HTTP verbs as methods and in response to a request will call the appropriate do method. SlingAllMethodsServlet将支持任何有效的 HTTP 动词作为方法,并且响应请求将调用适当的do方法。 For instance, in response to a PUT request the doPut method will be called.例如,为了响应PUT请求,将调用doPut方法。

In your case if you wanted your servlet to support both getting data and creating new data, you would want to allow methods GET and POST and implement the doGet and doPost methods.在您的情况下,如果您希望您的 servlet 支持获取数据和创建新数据,您将希望允许方法GETPOST并实现doGetdoPost方法。

@Service
@Properties( {
    @Property(name = "sling.servlet.paths", value = { "/getData" } ),
    @Property(name = "sling.servlet.methods", value = { "GET", "POST" } )
    } )
public class DataServlet extends SlingAllMethodsServlet {

    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) { ... }

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) { ... }

}

To add delete and edit support as well you would simply need to support the DELETE and PUT HTTP verbs along with implementing the doDelete and doPut methods in your servlet.要添加删除和编辑支持,您只需要支持DELETEPUT HTTP 动词以及在您的 servlet 中实现doDeletedoPut方法。

A tangentially related note - by using the @SlingServlet annotation in place of the @Service and @Component annotations you can shorten the code a bit and get better autocompletion and documentation support.切线相关的注意事项 - 通过使用@SlingServlet注释代替 @Service 和 @Component 注释,您可以稍微缩短代码并获得更好的自动完成和文档支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM