How set base package to scan subpackages for actions? - java

How can I tell to Struts 2 convention plugin to scan all subpackages of a package. I tried with this
<constant name="struts.convention.action.suffix" value="Controller" />
<constant name="struts.convention.package.locators.basePackage" value="fi.fpf.mvc" />
and this
<constant name="struts.convention.action.suffix" value="Controller" />
<constant name="struts.convention.package.locators.basePackage" value="fi.fpf.mvc.*" />
but they don't work. my actions end with "Controller" suffix. Someone knows how to do it?
here's my struts.xml:
<struts>
<constant name="struts.convention.exclude.parentClassLoader" value="true"/>
<constant name="struts.convention.action.fileProtocols" value="jar,vfs,vfsfile,vfszip"/>
<constant name="struts.convention.action.suffix" value="Controller" />
<constant name="struts.convention.package.locators.basePackage" value="fi.fpf.mvc" />
<package name="fpf-default" extends="struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
</package>
</struts>
and one action:
#Action("indexController")
public class IndexController extends ActionSupport{
private static final long serialVersionUID = -2613425890762568273L;
#Action(value="loadIndex", results={
#Result(name="indexView", location = "indexView", type="tiles")
})
public String loadIndex() {
return "indexView";
}
}

Try
<constant name="struts.convention.action.packages" value="fi.fpf.mvc.*"/>
if you are using convention plugin, then you should follow the class and package name conventions. Why not just name a base package to "struts" or "struts2" and with the default package locators it will be located. Also the classes should have names that matches "Action" suffix.
You can tell the Convention plugin to ignore certain packages using
the property struts.convention.exclude.packages. You can also tell the
plugin to use different strings to locate root packages using the
property struts.convention.package.locators. Finally, you can tell the
plugin to search specific root packages using the property
struts.convention.action.packages.
See the docs.
Alternatively, you could set the base package and locator that match this package and any package under the base
<constant name="struts.convention.package.locators.basePackage" value="fi.fpf.mvc"/>
<constant name="struts.convention.package.locators" value="fi,fpf,mvc"/>

Related

Using Struts2 convention plugin, but getting NULL value from namespace parameter in URL [duplicate]

<action name="/partner/*/*" class="webStoreAction">
<param name="saveUrl">true</param>
<param name="homeName">HomePartner</param>
<param name="partner">{1}</param>
<param name="partnerId">{2}</param>
<param name="homeFromMenu">false</param>
<result name="success" type="jsf">
/WEB-INF/pages/load.xhtml
</result>
</action>
Why localhost:8080/partner/partnerName matches with this action?
Try <constant name="struts.patternMatcher" value="regex"/> the only viable matcher for the constants above. This is usually used with advanced wildcards mode, explained here.
Advanced Wildcards
From 2.1.9+ regular expressions can be defined defined in the action name. To use this form of wild card, the following constants must be set:
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.patternMatcher" value="regex" />
The URL splitted into namespace / and action name partner/partnerName. The last matches a pattern compiled from the name in the action config using a wildcard mapper, which is a default mapper.

struts.convention.result.path is not working in Struts2

My current project structure is as follows
WebContent
WEB-INF
View
TestPage.jsp
other JSP pages...
My assignment is to put all JSP pages inside folder WEB-INF and do all relative changes in the project.
WebContent
WEB-INF
View
TestPage.jsp
other JSP pages...
So I have to update all result tag in struts.xml
<result name="success">/View/TestPage.jsp</result>
to
<result name="success">/WEB_INF/View/TestPage.jsp</result>
After search on web I found a plugin - struts convention plugin to achieve this, But it follows its Naming convention.
Can I override Struts convention plugin configuration (that will not follow its naming convention)?I have tried too, but it is not reflecting. My struts.xml is
<struts>
<constant name="struts.devMoade" value="true" />
<constant name="struts.convention.result.path" value="/WEB-INF/View/" />
<package name="test" extends="struts-default" namespace="/">
<action name="hello1" class="testAction.Hello1Action">
<result name="success">/TestPage.jsp</result>
</action>
</package>
</struts>
When I run
localhost:8080/project-name/hello1
It displays error 404.But if I change result in struts.xml as
<result name="success">/WEB-INF/View/TestPage.jsp</result>
It works fine.
I don't wanna do changes in all result tags.How can I achieve this by doing changes at one place?
The convention plugin uses a different configuration provider and this constant only works with configuration created by convention.
<constant name="struts.convention.result.path" value="/WEB-INF/View/" />
If you want to override the convention configuration you should use annotations.
package testAction;
#ParentPackage("json-default")
#Namespace("/")
#Action(value="hello1", results=#Result(name = "success", location="TestPage.jsp"))
public class Hello1Action extends ActionSupport {
}

REST Only - default-interceptor-ref doesn't work

I'm using this as a reference to create a REST only configuration on Struts2:
https://cwiki.apache.org/confluence/display/WW/REST+Plugin
My current problem is with interceptors. I created a sample interceptor that should be executed before the action gets hit.
Here it is:
public class AuthInterceptor extends AbstractInterceptor implements Interceptor
{
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("intercepting AuthInterceptor...");
return invocation.invoke();
}
public void destroy() {
System.out.println("Destroying AuthInterceptor...");
}
public void init() {
System.out.println("Initializing AuthInterceptor...");
}
}
and here's my struts.xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="true"/>
<constant name="struts.mapper.class" value="rest" />
<constant name="struts.convention.action.suffix" value="Controller"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<constant name="struts.convention.default.parent.package" value="rest-default"/>
<constant name="struts.convention.package.locators" value="controllers"/>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="myInterceptor" class="com.company.interceptors.AuthInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="myInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>
</package>
</struts>
The logs (catalina.out) say that my interceptor got initialized, but never actually intercepted anything.
Initializing AuthInterceptor...
Apparently the default-interceptor-ref doesn't work well with the rest mapper class. Is this the case? or am I doing something wrong here?
You need to change this
<constant name="struts.convention.default.parent.package" value="default"/>
<package name="default" extends="rest-default">
The convention plugin when creates action configuration inherits interceptors defined in the parent package. To define a parent package you can use #ParentPackage annotation or the constant above which defines a parent package for all actions.
The previous respuest is correct but for implement all REST functionality in the interceptor you must change your inteceptor stack to:
<interceptor-stack name="myStack">
<interceptor-ref name="myInterceptor"/>
<interceptor-ref name="restDefaultStack"/>
</interceptor-stack>

Calling a URL without forward slash end up to index.jsp in webapp folder struts2

With some help of the people here, I've managed to get my project to call the default actions of the packages without the suffix .htm. However, the request end up to index.jsp inside the webapp folder if I call the URL without the forward slash.
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.action.extension" value="htm,," />
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true"/>
...
<package name="home" namespace="/secured" extends="default">
<default-action-ref name="index" />
<action name="index" class="homeAction" method="execute">
<result name="success" type="tiles">home</result>
</action>
</package>
</struts>
Actions are executed if I call http://someurl/someproject/secured/, but calling the URL http://someurl/someproject/secured end up to the file index.jsp.
What to do? Thanks
When you are calling http://someurl/someproject/secured url the secured is treated like action w/o suffix, because you have configured that actions can have empty suffix (which is also default btw). If you want that this url redirects to /secured namespace you can declare secured action with redirectAction result in package with empty or / namespace.
<package name="..." namespace="/" extends="struts-default">
...
<action name="secured">
<result type="redirectAction">
<param name="actionName">index</param>
<param name="namespace">/secured</param>
</result>
</action>
...
</package>

I18n in struts2

I'm new in struts2 and I want to add i18n in my web application. I sea in documentation such code:
<s:url id="localeEN" namespace="/" action="locale">
<s:param name="request_locale">en</s:param>
</s:url>
<s:url id="localeruRU" namespace="/" action="locale">
<s:param name="request_locale">ru_RU</s:param>
</s:url>
<s:a href="%{localeEN}">English</s:a>
<s:a href="%{localeruRU}">Russian</s:a>
and we need to add action class like this:
public class LocalizationAction extends ActionSupport {
public String execute() {
return SUCCESS;
}
}
and in struts.xml we add this:
<struts>
<constant name="struts.custom.i18n.resources" value="global"/>
<constant name="struts.devMode" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<action name="locale" class="by.bulgak.newsmanager.action.LocalizationAction">
<result name="success">index.jsp</result>
</action>
</package>
</struts>
also I have property files with names global and global_ru_RU
I do everything that I read from tutorial but when I set param in my jsp page my IDE tells me that the name request_locale is unknown property..
please tell me where is my mistake.
my IDE don't sea it and thats why when I want to change language when I run my app my IDE don't call I18n methods in struts2
The Struts2 doesn't associate default i18n property with english language.
When I set default property like this: property_name_en.properties it working fine.
But I don't understand why it is so.
I see many examples and I even start them into my machine and they work fine,
but my didn't work.. May be someone know what is problem?

Categories

Resources