Codebehind plugin have access path as .action? - java

Can I able to access method as action in Struts 2 with Codebehind plugin? Since there is no struts.xml file and its works as action!method in the URL.
Just I'm wondering to access a method
How to use Struts tag on class?
"#Action(name="action"), <s:url action="action" method="method" />
Class :
#Action(name = "JSONexample")
#Result(name="success",type=JSONResult.class,value="",params={"root","List"})
public class PartAction extends ActionSupport {
public String JSONexample{
-----
return SUCCESS;
}
public String readxml { }
}
if I access below method on submit button click , will I get the JSON data?
$.getJSON('ajax/sayHi.action', formInput,function(data)
So i can access action in URL as
localhost:7001/Sample/JSONmethod.action
to get the JSON data ?

As far as Codebehind Plugin doesn't support actions on methods you need to use Convention Plugin then use convention annotation #Action on method you want to be your action then you don't need ! explicit mapping.
If you continue to use the Codebehind Plugin, then you have to use Dynamic Action Invocation (DMI) to call the method other than mapped to your action.
Another time had to reread you question, it seems you want to access the action that has not mapping to the method other that execute:
It isn't supported by the codebehind plugin;
You have not mapped it via struts.xml. In this case the answer would be - no, you can't. But you can always use execute method!
If you continue with using of DMI better use s:url tag to construct the URL and use the method attribute.

Related

Jenkins: Using validateButton on a class without Descriptor

I have a hudson.model.ManagementLink (which creates a page which can be accessed via Manage Jenkins.
I have some form fields which I'd like to validate using the f:validateButton. If this were to be done on say the config.jelly page of a Publisher (which has a descriptor) a simple doXXX() method would work in the descriptor.
If I add a similar doXXX() method directly to hudson.model.ManagementLink it is in fact accessible directly via the URL myPage/myMethod however the f:validateButton just returns a 404 and obviously I need to send the form values.
One potential solution I've come accross is withCustomDescriptorByName and have the logic in a descriptor I have elsewhere, but I can't get that to work.
To answer the actual question above (i.e. no descriptor), you can supply any fields needed for validation to the with attribute of f:validateButton (comma separated) then set the method attribute to something like the following...
method="${it.fullURL}triggerOfficial"
... then in the java (it) there's a method...
public String getFullURL(){
return Stapler.getCurrentRequest().getOriginalRequestURI().substring(1);
}
...then also the method to perform the validation itself...
public FormValidation doTriggerOfficial() {
return FormValidation.ok("hello");
}
However, if you want to perform normal field validation (without a validateButton) on a class type which doesn't normally have a descriptor...
1) Add implements Describable<YourClassName> to your class signature
2) Add something like this...
#SuppressWarnings("unchecked")
#Override
public Descriptor<ConfigLink> getDescriptor() {
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
throw new IllegalStateException("Jenkins has not been started");
}
return jenkins.getDescriptorOrDie(getClass());
}
3) Plus an inner class (with the normal doCheckXXX methods)
#Extension
public static final class DescriptorImpl extends Descriptor<YourClassName> {...}
4) Then finally to link it in the jelly add attribute descriptor="${it.descriptor}" to the f:form tag containing your form elements you want to have auto-validated (this will invoke the getDescriptor detailed in step 2)

s:form tag action parameters being removed

I have searched and searched and this is destroying me. I have this:
<s:form method="post" action="%{methodOne}" cssClass="buttons">
The emailFormUrl returns the URL correctly but the parameters have been stripped.
public String methodOne() {
return anotherClass.methodTwo(id);
}
Which speaks of:
public static String methodTwo(
String id) {
return fastEncode("", "longurl/view.jsp",
new ParameterPairing("id", id));
}
For some reason, the id is being stripped, this leaves me with a validation error and doesn't complete the action that I require. As I am aware we did not have a problem with it before the July urgent security update but it is small functionality that is rarely used (an argument for its removal I guess).
I don't want to add a hidden parameter as I want to understand the reason that this is not working, not a workaround (I am still in the heavy learning part of my career).
In servlet environment the <s:form> tag uses ServletUrlRenderer class to render form url.
If configuration for the action specified in action attribute cannot be found then literal value (w/o parameters) of an action attribute will be used.
Note: you need to use action name w/o extension in order that it can be found in configuration. So some_action?foo=bar will be set with parameters in form if you have some_action in configuration, but some_action.action?foo=bar won't be found because of .action extension and parameters will be stripped.

Defining different validations based on the action method which is called in a Struts2 action

I'm creating a web application with Struts 2. In a JSP file, I created a form like this :
<s:form action="actionclassname!%{methodToCall}" method="post">
...
</s:form>
In the action class, I created two methods which will be called depending on the value of the variable methodToCall. That works well.
My problem is that the two methods of the action class need some validations. So I used annotations to validate the form if the first method is called :
#Validations(....)
public String actionMethod1(){
...
}
#Validations(...)
public String actionMehod2(){
...
}
The first method works well with validations. My problem is on the second method. It seems like when I define validations (also using annotations) for this second method, the validations I defined on first method are executed again before those I defined for the second method.
How to make validations on the second method to run when it's this method which is called by the JSP?
In the action configuration via annotation you should use a parameter validateAnnotatedMethodOnly for validation interceptor, like in this example
#Action(value="actionclassname", results = {
#Result(name="input", location = "/actionclassname.jsp")
},interceptorRefs = #InterceptorRef(value="defaultStack", params = {"validation.validateAnnotatedMethodOnly", "true"}))

How can I call a JSP 2.0 *.tag file from a traditional JSP custom tag?

I have written a JSP custom tag, implemented in a class that extends TagSupport, and I'd like to invoke another custom tag that was written as a *.tag file. Is this possible? If so, how can it be done?
You can't, tag files can only be executed from a JSP.
Even executing one tag class from another is questionable, you're not supposed to do that either (although it would probably work).
If you need reusable logic that gets invoked from your tag class, then you either need to extract it out into a common class, or else you need to make your custom tag into a BodyTag, and put the .tag file in as nested content withoin it, e.g. if tagX is a taglib, and tagY is a tag file, then:
<mylib:tagX>
<mytags:tagY/>
</mylib:tagX>
The output content of <mytags:tagY/> would then be exposed to the tagX code via the BodyTag interface.

How do I access init parameters from a JSP2 tag file?

I'm trying to create a jsp tag file but it fails to compile when I try to use pageContext.getServletConfig().getInitParameter("myInitParam")
I'm using tomcat and when I try to view a page including the file I get a jasper compile error pageContext cannot be resolved. I've also tried just using getInitParameter but it fails also. I can use the request object so I know everything else is fine.
Does anyone know a way to access init parameters set in the web.xml from a jsp tag file, preferably from within a scriptlet?
I just found out the trick is to use one of the implicit objects, in this case config or application depending on the init-parameters scope.
they are list at http://today.java.net/pub/a/today/2003/11/14/tagfiles.html
Have you tried the request rather than the pageContext? Or just off the servlet itself:
getInitParameter("myInitParam");
Are you extending the TagSupport class?
If so, this class has a member named pageContext, the Tag interface declares a method setPageContext(PageContext pc), which the docs state
This method is invoked by the JSP page implementation object prior to doStartTag().
So you should be able to reference this.pageContext fine - unless you are extending a different class?
application.getInitParameter("<Name>");

Categories