Project Structure in Eclipse
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Struts2Starter</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="getTutorial" class="org.awanish.action.TutorialAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
ActionClass
package org.awanish.action;
public class TutorialAction {
public String execute() {
System.out.println("Hello from execute");
return "success";
}
}
When running the above project in tomcat-8.0.43 container, getting my welecome page but when tried to test the action in browser http://localhost:8080/Struts2Starter/getTutorial.action it gives this
Error in browser
1.check in logs if:Hello from execute is printed
2. check if the jsp exists in the correct path
3. .action is not required at the end
4.<action name="getTutorial" class="org.awanish.action.TutorialAction" method="define your own method and try calling it">
5.Add jars at runtime also inside WEB-INF/lib folder
http://localhost:8080/Struts2Starter/getTutorial
And if you begineer try using struts 2.3 rather than struts 2.5 ,you will get lots of example:https://www.mkyong.com/struts2/struts-2-hello-world-example/
Download code and build on top of it if still facing issue
Related
I am trying to use Tile plugin in my struts2 application, but I am receiving the following error,
"Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException:
ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: java.lang.ClassNotFoundException: org.apache.struts2.tiles.StrutsTilesListener. Please see server.log for more details."
I have the following jar files:
tiles-api-3.0.1.jar
tiles-autotag-core-runtime-1.1.0.jar
tiles-compat-3.0.1.jar
tiles-core-3.0.1.jar
tiles-el-3.0.1.jar
tiles-extras-3.0.1.jar
tiles-freemarker-3.0.1.jar
tiles-jsp-3.0.1.jar
tiles-mvel-3.0.1.jar
tiles-ognl-3.0.1.jar
tiles-request-api-1.0.1.jar
tiles-request-freemarker-1.0.1.jar
tiles-request-jsp-1.0.1.jar
tiles-request-mustache-1.0.1.jar
tiles-request-servlet-1.0.1.jar
tiles-request-servlet-wildcard-1.0.1.jar
tiles-request-velocity-1.0.1.jar
tiles-servlet-3.0.1.jar
tiles-template-3.0.1.jar
tiles-velocity-3.0.1.jar
commons-beanutils-1.8.0.jar
commons-digester-2.0.jar
jcl-over-slf4j-1.5.8.jar
slf4j-api-1.5.8.jar
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
<action name="register">
<result type="tiles">register</result>
</action>
</package>
</struts>
My struts library files
Struts 2.3.4 Core Libraries - xwork-core-2.3.4.jar
Struts 2.3.4 Core Libraries - struts2-core-2.3.4.jar
Struts 2.3.4 Core Libraries - struts2-convention-plugin-2.3.4.jar
Struts 2.3.4 Core Libraries - ognl-3.0.5.jar
Struts 2.3.4 Core Libraries - javassist-3.11.0.GA.jar
Struts 2.3.4 Core Libraries - freemarker-2.3.19.jar
Struts 2.3.4 Core Libraries - commons-lang3-3.1.jar
Struts 2.3.4 Core Libraries - commons-io-2.0.1.jar
Struts 2.3.4 Core Libraries - commons-fileupload-1.2.2.jar
Struts 2.3.4 Core Libraries - asm-tree-3.3.jar
Struts 2.3.4 Core Libraries - asm-commons-3.3.jar
Struts 2.3.4 Core Libraries - asm-3.3.jar
Yet another question on this I know, but let me just say I looked everything else or about. So, I'm currently building the structure of my Struts 2 Project. I use Struts 2.3.4, JBoss 7.1 and Eclipse Indigo. I also included all the necessary jar into my project. The problem is when I run my application (Add project to Jboss, Run On Server) and go "http://localhost:8080/booxstore/" I get this:
Etat HTTP 404 - There is no Action mapped for namespace [/] and action name [] associated with context path [/booxstore]
My web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Booxstore</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
My struts.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<package name="pages" namespace="/pages" extends="struts-default">
<action name="afficher" class="com.booxstore.controller.DisplayAction" >
<result name="SUCCESS">/center.jsp</result>
</action>
</package>
</struts>
My action :
/**
*
*/
package com.booxstore.controller;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class DisplayAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 8199854978749642334L;
public String execute() throws Exception{
System.out.println("IN");
return Action.SUCCESS;
}
}
My JSP just have plain text and have imported struts 2 tags. Basically no matter what I do I can't get a resource. If I go to "http://localhost:8080/booxstore/" or "http://localhost:8080/booxstore/DisplayAction" I get "There is no action mapped" and if I tried to access directly the jsp I get a 404. Please note my war is deployed and enabled on the server.
Put index.jsp under WebContent folder, move pages folder to WEB-INF and then change the struts.xml with:
<struts>
<constant name="struts.devMode" value="false" />
<package name="pages" extends="struts-default">
<action name="afficher" class="com.booxstore.controller.DisplayAction" >
<result name="SUCCESS">/WEB-INF/pages/center.jsp</result>
</action>
</package>
</struts>
I think that the problem is that the application is not finding the jsps that you are putting in the configuration files. And META-INF folder is not the place for putting your jsps pages if you don't want to make them public, WEB-INF is better for that purpose.
To prevent misunderstandings about mappings, add the config browser plugin jar to your application. It will allow you to browse all pages. Caution! Only deploy the dependency in dev or testing, never ond production. Use maven profiles for this restrictions.
http://struts.apache.org/release/2.3.x/docs/config-browser-plugin.html
I am trying to run this example using struts 2. I inserted the libraries in the Lib folder of Web-Inf of a dynamic web project in eclipse.
My Web.xml looks like this now:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
and i created the struts.xml and placed it inside the source folder , the content of which is
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="UserAction" class="struts.UserAction">
<result name="input">/index.jsp</result>
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
So now when i run any jsp i get the following error in eclipse console
Unable to load configuration. - bean - jar:file:/D:/Users/Don/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/Mscs674-Struts2/WEB-INF/lib/struts2-gxp-plugin-2.3.4.jar!/struts-plugin.xml:8:162
and this on the webpage
HTTP Status 404 -
type Status report
message
description The requested resource () is not available.
Any suggestions ?
The best way i found to create struts 2 application is to use the struts-blank.jar file. It has all the required configurations done for you, and you can remove some example files. It will have all the jars and xml files created for you.
If you still have problems, try doing this now...
first remove all jar (adding all lib is not solution)
Only add necessary libraries
so problem is you are having too many lib
Seems like some jars are missing .. !!
The common dependecies are ::
commons-fileupload-1.2.1.jar
- commons-io-1.3.2.jar
- commons-logging-1.0.4.jar
- commons-logging-api-1.1.jar
- freemarker-2.3.15,jar
- log4j-1.2.14.jar
- ognl-2.7.3.jar
- xwork-core-2.1.6.jar
Check the version number and add jars to classpath.
You do One thing..add javassist-3.11.0.GA.jar file.it will remove your error like
Dispatcher initialization failed java.lang.RuntimeException:
java.lang.reflect.InvocationTargetException ..
I hope this will help you..
Can some one help me on how to exclude decoration of child window? Here is my code:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Hello World Struts 2 Maven</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
</listener>
<!--Filters -->
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter>
<filter-name>struts-execute</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>struts-execute</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
</web-app>
struts.xml:
<struts>
<constant name="struts.devMode" value="true" />
<package name="basicstruts2" extends="struts-default">
<!-- If no class attribute is specified the framework will assume success and
render the result index.jsp -->
<!-- If no name value for the result node is specified the success value is the default -->
<action name="index">
<result>/index.jsp</result>
</action>
<action name="child3">
<result>/WEB-INF/popUp/Test.jsp</result>
</action>
<!-- If the URL is hello.action the call the execute method of class HelloWorldAction.
If the result returned by the execute method is success render the HelloWorld.jsp -->
<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
decorators.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<decorators defaultdir="/decorators">
<excludes>
<pattern>/WEB-INF/popUp/Test.jsp</pattern>
<pattern>/popUp/Test.jsp</pattern>
<pattern>child3.html</pattern>
<pattern>child3</pattern>
<pattern>/default/child3</pattern>
<pattern>/default/child3.html</pattern>
</excludes>
<decorator name="main" page="layout.jsp">
<pattern>/*</pattern>
</decorator>
<decorator name="panel" page="panel.jsp" />
</decorators>
I have already spent 2 days on this task and not succeeded yet. Can anyone help or point to me to some links?
Regards,
Nazir
I solved the problem. I added struts2-sitemesh plugin and followed the documentation on configuring the web xml. Now my decorators are working well.
I am using strust2 for my web application development. My struts.xml file will be like:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default" namespace ="/">
<action name="signup">
<result>/check.jsp</result>
</action>
</package>
</struts>
and my web.xml file will be something like:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>sample</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>My WS</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.dr1.dr2</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>My WS</servlet-name>
<url-pattern>/checking</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
and now,
if i access like: http://localhost:8080/appName/ its going to my signup action perfectly.
But when i tried to access like http://localhost:8080/appName/checking (for webservices), Its even looking in struts.xml and getting an error message like:
HTTP Status 404 - There is no Action mapped for namespace / and action name checking...
even after defining this in web.xml..
Is there any way to exclude a pattern in struts2, so that when I hit http://localhost:8080/appName/checking, it must not look struts action, it must call my default page path defined in web.xml file.
Thanks..
you can probably add exclude pattern in your struts.xml, something like
<struts>
<constant name="struts.action.excludePattern" value="appName/checking"/>
</struts>
read here and find excludePattern for more information.
this could works:
<!-- exclude pattern -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/struts/*</url-pattern>
</filter-mapping>
an action exampe:
<constant name="struts.action.excludePattern" value="appName/checking"/>
the constant excludePattern explicit exclusions (since 2.1.7)