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

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 {
}

Related

Struts2: Setting constant struts.action.extension to "," not working

I am working on struts2-archetype-starter. Got a demo struts 2 project by executing:
mvn archetype:generate -B -DgroupId=demoStrutsStarter -DartifactId=DemoStrutsStarter -DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts2-archetype-starter
I modified my struts.xml and added a struts.action.extension constant declaration. Now it looks like this:
<?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.action.extension" value=","/>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<package name="myPackage" extends="struts-default">
<default-action-ref name="index" />
<action name="index" class="demoStrutsStarter.IndexAction">
<result>/jsp/index.jsp</result>
</action>
<action name="helloWorld" class="demoStrutsStarter.HelloWorldAction">
<result name="input">/jsp/index.jsp</result>
<result>/jsp/helloWorld.jsp</result>
</action>
</package>
</struts>
But, I am getting a 404 error when I type:
http://localhost:8080/DemoStrutsStarter/index
I am not sure where I am wrong. Can anybody please help me find out why I am unable to call actions without .action extension?
Try adding a namespace to your package:
<package name="myPackage" namespace="/" extends="struts-default" >
Try this out in both results
result 1
<result>/WEB-INF/jsp/index.jsp</result>
result 2
<result>/WEB-INF/jsp/helloWorld.jsp</result>
else check your package name is correct or not.
by default, it's name default
try this links. it may help
1.stack overflow link 1
2.link 2 mkyong
3.stack overflow link 3

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>

How set base package to scan subpackages for actions?

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"/>

How to remove the extensions from url

I have designed an application using struts2, and need to remove the extensions for example
www.myweb.com/register.action
How to remove the .action so when I click on register it call www.myweb.com/register without action extension?
I use the following
Register
I've found the answer. I should change the struts configuration as following:
<struts>
<constant name="struts.action.extension" value=""/>
<package name="default" extends="struts-default">
<action name="register">
<result type="tiles">register</result>
</action>
</package>
</struts>

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