Get current view in FormValidation

45 views
Skip to first unread message

Jochen Fuerbacher

unread,
Mar 10, 2016, 4:47:32 AM3/10/16
to Jenkins Developers
Hi,

is there a possibility to get the current view in a FormValidation?

I want to extend the view configuration (with ViewProperty). Therefore I created some fields.

Now I need the current view in my FormValidation method. There's an annotation @AncestorInPath, but this only works for items and not for views.

Is there any other annotation to get the current view? I don't mean the fields of the view, but something like this:
public FormValidation doCheckView(@AncestorInPath View view, @QueryParameter String value) {

//Do something with view.
...
}


Thank you very much!
Jochen Fürbacher

Jesse Glick

unread,
Mar 10, 2016, 8:09:22 AM3/10/16
to Jenkins Dev

AFAIK non-default `View`s should be in the ancestor list.

Jesse Glick

unread,
Mar 10, 2016, 8:13:30 AM3/10/16
to Jenkins Dev

Tip: from `mvn hpi:run` dev mode, look at HTTP headers to see Stapler traces.

Jochen Fuerbacher

unread,
Mar 11, 2016, 3:14:02 AM3/11/16
to Jenkins Developers
Thank you! Unfortunately views aren't in the ancestor list.

At the moment I do this to get the view:
public FormValidation doCheckView(@QueryParameter String value) {            
           
String viewpath = Stapler.getViewURL(View.class, "Testview");
           
String viewname = viewpath.substring(viewpath.lastIndexOf("/")+1);
           
View view = Jenkins.getActiveInstance().getView(viewname);
           
...
}

However, this way isn't very elegant. Is there any more smart way to get the view?

Robert Sandell

unread,
Mar 11, 2016, 9:34:27 AM3/11/16
to jenkin...@googlegroups.com
Well you seem to be mixing up two different concepts

Stapler.getViewURL gets you the resource URL to a class' view (.jelly or .groovy) Is it that type of view you want?

I'm guessing you're after the current hudson.model.View that is currently being configured?

You can get that via the current ancestor path (the path of domain model objects that got to the execution of your "doX" method)

Stapler.getCurrentRequest().findAncestor(View.class).getObject()

/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-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/8e62679f-a289-45fc-8c2f-675d028722cb%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Robert Sandell
Software Engineer
CloudBees Inc.

Jesse Glick

unread,
Mar 11, 2016, 3:56:21 PM3/11/16
to Jenkins Dev
On Fri, Mar 11, 2016 at 9:34 AM, Robert Sandell <rsan...@cloudbees.com> wrote:
> Stapler.getCurrentRequest().findAncestor(View.class).getObject()

Or more simply, `@AncestorInPath View view` from a web method.

However if you are doing this from a `FormValidation doCheckXXX` web
method called from a lazy-loaded block (such as a newly configured
property), you are probably hitting JENKINS-19413. Workaround:

https://github.com/jenkinsci/workflow-plugin/blob/82e7defa37c05c5f004f1ba01c93df61ea7868a5/step-api/src/main/java/org/jenkinsci/plugins/workflow/util/StaplerReferer.java#L61

Jochen Fuerbacher

unread,
Mar 21, 2016, 2:58:34 AM3/21/16
to Jenkins Developers
Thank you very much! Yes, I tried doing `@AncestorInPath View view` from a lazy-loaded block. Ok, then I need to do the workaround.
Reply all
Reply to author
Forward
0 new messages