Before I used struts.xml for configuration my struts2-application. But new my application I need to implement using annotations.
For my action class I used such annotation
#Namespace("/public")
#InterceptorRefs(value = { #InterceptorRef(value = "authInterceptor"), #InterceptorRef(value = "defaultStack") })
#ResultPath(value = "/")
#Result(name = "error", location = "/test/error.jsp")
In struts.xml I set name for the interceptor class and then used it in interceptor-stack. So I see that for my Action class I can set the name of interceptor (value = "authInterceptor"), but where I can set this name for my interceptor class?????
Or using annotations cann't implement configuration of interceptors?
I've not seen configuration of interceptors done outside of xml. You'll find your annotation work a lot easier if you use the conventions plugin. By following some simple rules, you can avoid any xml or annotations by just following nameing conventions. Then only where you must deviate from those conventions will you find yourself using annotations.
Related
So I have a need for injecting a number of different services into an authorization attribute I'm using. For simplicity I will leave this to show the configuration manager.
public class FeatureAuthorizeAttribute : AuthorizeAttribute
{
public IConfigurationManager ConfigurationManager;
private readonly string _feature;
public FeatureAuthorizeAttribute(string feature)
{
_feature = feature;
var test = ConfigurationManager.GetCdnPath();
}
}
Which would be used as follows
[FeatureAuthorize("Admin")]
I have tried to use constructor injection
public FeatureAuthorizeAttribute(string feature, IConfigurationManager configurationManager)
{
ConfigurationManager = configurationManager;
_feature = feature
}
However this just causes an error when I attempt
[FeatureAuthorize("Admin", IConfigurationManager)]
Which seems like the wrong way to go about it in the first place. I'm assuming that I need to register my custom authorization attribute with the container to get it to start picking up
Instead of trying to use Dependency Injection with attributes (which you can't do in any sane, useful way), create Passive Attributes.
Specifically, in this case, assuming that this is an ASP.NET MVC scenario, you can't derive from AuthorizeAttribute. Instead, you should make your Authorization service look for your custom attribute, and implement IAuthorizationFilter. Then add the filter to your application's configuration.
More details can be found in this answer: https://stackoverflow.com/a/7194467/126014.
I'm developing a plugin that needs a specific index configuration in table mapping.
static mapping = {
myProperty index:'myProperty_Idx'
}
Is it some way to let plugin users decide if they want to use or not this mapping via Config.groovy file?
I believe you can read the config variables right from your mapping block.
So this line in the app consuming your plugin's Config.groovy
grails.myplugin.useIndexForFoo = true
Should allow you to have a configurable domain class, such as
class Foo
{
String myString
static mapping = {
if (Holders.config?.grails?.myplugin?.useIndexForFoo)
myString index: "myString_idx"
}
}
Note I have used Holders rather than injecting grailsApplication bean because mapping config is static - do not know if this is optimal or not
I have came across an issue where i am unable to find a solution.I am working on a web-application and have to impliment Oauth, things are working fine for me except one issue,in my redirect back URL from Yahoo i am getting several parametersand i need to access few of them in my action class.
Now i can easily create a property in my action class with its getter and setter methods but the name of the property is
openid.response_nonce
and my Eclipse editor will not allow me to name a variable like this.Though one solution is add RequestAware interceptor in my action class and access the parameter.
my Question is can i access it without using RequestAware inteceptor?
There isn't a RequestAware interceptor... There is a Servlet-Config interceptor which will check if your action has one of the following interfaces: ServletContextAware, ServletRequestAware, ServletResponseAware, ParameterAware, RequestAware, SessionAware, ApplicationAware, PrincipalAware.
The Servlet-Config interceptor is part of the default-stack, which you are probably already using. So there is no additional cost or configuration required to use one of the aware interfaces.
That aside, if you have a parameter called "openid.response_nonce" which contains a string, you should be able to refer to it with:
//following not tested, and not checked for syntax errors
private Map openid = new HashMap();
//In Constructor{
oauth.put("response_nonce","");
}
//create BOTH a getter and setter for openid
public getOpenid(){
return openid;
}
public setOpenid(Map openid){
this.openid = openid;
}
Now struts2 should be able to figure out how to set the value... I think, sorry didn't test it. You could always create a class called Openid with a response_nonce property(along with the appropriate getters and setters for that Class)... but I think in this case it might be best to just use RequestAware if you only need that single property.
I think that you maybe looking for the Alias interceptor. http://struts.apache.org/2.0.14/docs/alias-interceptor.html
Regards
I want to modify the response content of specific Grails requests. How do I configure a ResponseWrapper in a request filter?
I had hoped it would be the following, but the response is a read-only property:
class MyFilters {
def filters = {
wrapFoo(controller:'foo', action:'bar') {
before = {
response = new MyResponseWrapper(response)
}
[...]
Thanks!
You can't - Grails filters are wrappers for Spring HandlerInterceptors and are invoked further up the processing chain than servlet filters. If you want to wrap the response you need to use a real servlet filter.
Create the class in src/java or src/groovy that implements javax.servlet.Filter and register it in web.xml like you would in a non-Grails application. To get access to web.xml run grails install-templates and edit the file in src/templates/war
I know Grails has a map based constructor for domain objects, to which you can pass the params of a URL to and it will apply the appropriate field settings to the object using introspection, like this...
myDomainInstance = new MyObject(params)
I was wondering whether there was an equivalent method of taking the params and applying them to an existing object and updating values in the same way that the map constructor must work, something like...
myDomainInstance = params
or
myDomainInstance = fromParams(params)
Am I just wishful thinking or does such a thing exist? I can code it up myself but would rather not if it exists already.
Thanks
Adapted from the grails user guide:
obj = MyObject.get(1)
obj.properties = params
Check out the documentation for 'params' under the controller section for more information.
It really depends on what you are trying to do but an equivalent approach use databinding.
def sc = new SaveCommand()
bindData(sc, params)
This give you the benefit of using custom binding. If let say your date format is not the default one you can redefine it through a bean like this:
public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
}
}