How to implement doIndex() method from Jenkins RootAction class

48 views
Skip to first unread message

Cheng Bao

unread,
Sep 1, 2016, 12:12:00 PM9/1/16
to Jenkins Developers


I create my own implementation of RootAction in Jenkins, instead of default behavior that just render and display index.jelly, I want to do some operations. as states from http://stapler.kohsuke.org/reference.html

Index Action Method This is a slight variation of above. If there's no remaining URL and there's an action method called "doIndex", this method will be invoked. Formally, evaluate(node,[]) := node.doIndex(...)

my code is

@Extension
public class MyRootAction implements RootAction {
   public void doIndex(StaplerRequest req, StaplerResponse rsp) {
        // doing my work when user type /MyRootAction in browser bar
        // this method never executed
   }

   public void doOtherWork(StaplerRequest req, StaplerResponse rsp) {
        // doing my work when user type /MyRootAction/otherWork in browser bar
        // this method works as intended
   }
   //...
}


Any one can help me to point what I did wrong?

Robert Sandell

unread,
Sep 2, 2016, 5:20:27 AM9/2/16
to jenkin...@googlegroups.com
That looks to be correct, unless you have added a view called index.jelly or index.groovy because I think those are evaluated before doIndex.

/B

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/bfa2a630-6407-4e77-9e3c-68e47ad96b0a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Robert Sandell
Software Engineer
CloudBees Inc.

Stephen Connolly

unread,
Sep 2, 2016, 10:48:07 AM9/2/16
to jenkin...@googlegroups.com
Well taking the stapler response as a parameter is generally considered old-style, better is to return a HttpResponse

Other than that, it should work... What candidate views does it name on the 404 debug output?
--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/bfa2a630-6407-4e77-9e3c-68e47ad96b0a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Sent from my phone

Daniel Beck

unread,
Sep 2, 2016, 10:48:07 AM9/2/16
to jenkin...@googlegroups.com
It's not in the code you're showing us. I just created a new plugin that consists of just the following (+ imports in the code, and non-code boilerplate from the archetype)…

------
@Extension
public class MyRootAction implements RootAction {
@Override
public String getIconFileName() {
return "document.png";
}

@Override
public String getDisplayName() {
return "My Root Action";
}

@Override
public String getUrlName() {
return "myroot";
}

public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException {
rsp.sendError(500, "This message shown by doIndex");
}
}
------

And when clicking the side panel link, the expected happens:

------
HTTP ERROR 500

Problem accessing /jenkins/myroot/. Reason:

This message shown by doIndex
Powered by Jetty://
------

Are you sure it doesn't execute your method?
> --
> You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages