struts2 web application not found - java

I have created a struts 2 application and deploy it in tomcat by creating a war file. There is nothing wrong with the file structure as below is how it display under tomcat after deploy.
css
META-INF
User
pages
login.jsp
customer_list.jsp
WEB-INF
classes
struts.xml
lib
web.xml
But it gives the below error when i try to access the web site,
HTTP Status 404 The requested resource (/LoginApplication/User/Login) is not available.
But i found when tomcat start it gives below exception. But the jar file that claims to say missing is in right path.
Unable to load configuration. - bean - jar:file:/C:/tomcat6/webapps/LoginApplication/WEB-INF/lib/struts2-core-2.3.1.2.jar!/struts-default.xml:54:89
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:449)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
Below is the struts.xml file,
<struts>
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/User" extends="struts-default">
<action name="Login">
<result>pages/login.jsp</result>
</action>
<action name="Welcome" class="loginapplication.action.UserLoginAction">
<result type="redirect" name="SUCCESS">Customers</result>
<result name="input">pages/login.jsp</result>
</action>
<action name="Customers" class="loginapplication.action.ViewCustomerAction">
<result name="SUCCESS">pages/customer_list.jsp</result>
</action>
</package>
</struts>

The error indicates you have got problems with struts.xml file. Pls post that here. Also use Maven for managing dependency. You will never have to bother about folders then.
Pls verify the libraries and versions in your lib.
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.6.jar
xwork-2.0.1.jar

The namespace for the requested action Login is User. The URL should be like the below
http://localhost:8080/MyApp/User/Login
Configure the XML
<package name="User" namespace="/User" extends="struts-default">
<action name="Login">
<result>pages/login.jsp</result>
</action>
</package>
Note that package name and namespace are same

Related

Struts namespaces and WEB-INF folder [duplicate]

This question already has an answer here:
Struts2 URL unreachable
(1 answer)
Closed 7 years ago.
I want to put view.jsp file in WEB-INF folder. it lies in WEB-INF\user\view.jsp
<package name="user" extends="struts-default" namespace="user">
<action name="view" class="com.example.user.ViewUserAction">
<result>/WEB-INF/user/view.jsp</result>
</action>
</package>
but Struts changes this url to application/user/WEB-INF/user/view.jsp
so as I understand if my namespace is not "/" I will never visit the JSP in WEB-INF ?
I found where I was wrong.
As Alexander M pointed out in comments no such url will be generated.
The thing is I've tried various bad solutions, but most of them had the next synopsis.
In struts.xml I wrote:
<package name="product" namespace="/" extends="struts-default">
<action name="view" class="com.example.action.product.ViewProduct">
<result name="success">/WEB-INF/view/product/view.jsp</result>
</action>
</package>
and in jsp I refer to somee action as:
View product
but seems like its wrong way to do it - in my case I had urls like:
http://localhost:8080/app/product/product/view.action
which is not valid.
So I changed struts.xml namespace to:
<package name="product" namespace="/product" extends="struts-default">
<action name="view" class="com.example.action.product.ViewProduct">
<result name="success">/WEB-INF/view/product/view.jsp</result>
</action>
</package>
And then in view.jsp used struts <s:a> tag (or you may use <s:url>):
View product
<s:a action="view" namespace="/product"> View product</s:a>
For further reference about packages and namespaces understanding:
Struts Namespace Configuration
Struts Package Configuration

Exception without using velocity in Struts 2 while parsing struts.xml

Here is my struts.xml file
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<package name="Authentiate" extends="struts-default">
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<action name="loginAuthenticate*" class="com.authenticate.actions.LoginAuthenticate" method="{1}">
<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
<result name="redirectRegister" type="redirect">/registration.jsp</result>
</action>
</package>
I haven't used velocity templates but I am getting the following error.
java.lang.RuntimeException: com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found for dependency [type=org.apache.struts2.views.velocity.VelocityManager, name='default'] in public void org.apache.struts2.osgi.OsgiConfigurationProvider.setVelocityManager(org.apache.struts2.views.velocity.VelocityManager). - Class: com.opensymphony.xwork2.inject.ContainerBuilder
First of all try to point result type explicitly using default type dispatcher:
<result name="success" type="dispatcher">/welcome.jsp</result>
<result name="error" type="dispatcher">/error.jsp</result>
<result name="redirectRegister" type="redirect">/registration.jsp</result>
If this doesn't help, it looks like there are some struts dependencies that can't be resolved. According to this bug at apache's bugtracker you should add VelocityManager bean to your struts.xml:
<bean class="org.apache.struts2.views.velocity.VelocityManager" name="default" optional="true" />
This is really weird error. Fixed by removing spring-struts jar from dependencies.
Velocity is not necessary to add as a dependency if you are not using velocity templates in your results. By default Struts2 uses Freemarker templates.
Errors may not seen if your server not utilizing JMX, but if you run in JMX enabled appserver like JBOSS it might raise.

Struts 2 ajax validation unable to find interceptor class jsonValidationWorkflowStack

I'm trying to implement ajax validation in my Struts 2 application. I have included struts2-json-plugin in the build path. Whenever I start the server in Eclipse, I get the following error:
Unable to find interceptor class referenced by ref-name
jsonValidationWorkflowStack - interceptor-ref -
file:/C:/path/struts.xml:15:60
This is what my struts.xml looks like:
<struts>
<constant name="struts.url.includeParams" value="all" />
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
<action name="submitForm" class="action.FormAction" >
<interceptor-ref name="jsonValidationWorkflowStack"/>
<result>results.jsp</result>
<result name="input">index.jsp</result>
<result name="error">index.jsp</result>
</action>
</package>
</struts>
Why is this happening? I'm following the official struts2 ajax validation tutorial at.
You need to extend the "json-default" package.
The "struts-default" package doesn't know about the "jsonValidationWorkflowStack".

Multiple Struts 2 configuration files

I have multiple XML configs.
struts.xml
<struts>
<include file="struts-user.xml" />
<package name="baseInterceptors" extends="struts-default">
<interceptor name="...">
...
</interceptor>
...
</package>
<package name="default" extends="struts-default,baseInterceptors">
<action name="...">
...
</action >
...
</package>
</struts>
struts-user.xml
<struts>
<package name="user" extends="struts-default,baseInterceptors">
<action name="...">
...
</action >
...
</package>
</struts>
But the interceptors from baseInterceptors are not available in struts-user.xml. How can I solve this problem?
Configuration files are processed in order: The <include> is processed before the baseInterceptors and default packages are processed (read: defined).
In other words, the included file depends on pacakges that are not yet defined.
The package configuration docs explain this in a note near the top with an exclamation point next to it.
Unrelated, but you've duplicated some configuration, which is misleading/uncommunicative: baseInterceptors already extends struts-default, so there's no need to extend both. Consider creating something like an application-default package so it's obvious everything in the app should extend from it. This eliminates unnecessary duplication and communicates your intent.

struts2.xml - can't include other .xml file

I am using struts2, for that my struts.xml file contains code like :
<?xml version="1.0" encoding="UTF-8" ?>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<include file="strutsAuthentication.xml"/>
<package name="default" extends="struts-default">
<interceptors>
<interceptor-stack name="crudStack">
<interceptor-ref name="checkbox" />
<interceptor-ref name="params" />
<interceptor-ref name="static-params" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
</package>
And i have specified all the required actions inside the strutsAuthentication.xml. That code is :
<struts>
<package name="authentication" extends="default" namespace="/authentication">
<action name="saveCountry" class="saveCountryAction">
<interceptor-ref name="defaultStack" />
<result name="success">/savecountry.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
When i am deploying my application into tomcat, it gives me warning that :
WARN (org.apache.struts2.components.Form:308) - No configuration found for the specified action: 'saveCountry' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
It means struts.xml can't include strutsAuthentication.xml. Anyone have a solution ?? Thanx in advance....
Got d solution.... For above problem i was done a mistake in calling the action from jsp page. So namespace name "authentication" should be included at the time of calling the action class. Final solution is : "authentication/saveCountry.action".
I don't know what version of struts2 you're using but if you're using the 2.1.x branch you should look at the convention plugin http://cwiki.apache.org/S2PLUGINS/convention-plugin.html. You can get rid of 99% XML configuration.
As an add note. We should never use "xyz.action" in JSP pages. If we later need or decide to change the url-pattern from .action to .do or .html etc. We have to change all JSP pages. A better approach to compose links is:
Link Text

Categories