Struts2 + tiles

  • Creatore Discussione Creatore Discussione andriun
  • Data di inizio Data di inizio

andriun

Nuovo Utente
14 Nov 2014
5
2
0
Salve, sto eseguendo alcune modifiche a scopo didattico su un progetto di prova che ho creato sbirciando qua e la sui vari siti.

Subito dopo l'autenticazione che effettuo dalla maschera di login(login.jsp) che ho impostato, ottengo il seguente messaggio di errore:

GRAVE: Could not find action or result
/webOnEclipse/login
There is no Action mapped for namespace [/] and action name [login] associated with context path [/webOnEclipse]. - [unknown location]
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:552)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)



web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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>Archetype Created Web Application</display-name>
<display-name>Struts2 Application</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>Login.jsp</welcome-file>
</welcome-file-list>
<!-- <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>

<context-param>
<param-name>tilesDefinition</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
</web-app>
<?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.xml

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="default" extends="struts-default" namespace="/">
<result-types>
<result-type name="tiles"
class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="customer-form">
<result name="success" type="tiles">customer.tiles</result>
</action>
<action name="file-form">
<result name="success" type="tiles">file.load.tiles</result>
</action>
</package>

<include file="struts-login.xml"></include>
<include file="struts-customer.xml"></include>
<include file="struts-load.xml"></include>


<!-- <action name=""> -->
<!-- <result type="redirectAction">LoginAction</result> -->
<!-- </action> -->

</struts>

struts-login.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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="packlogin" extends="struts-default" namespace="/first">
<result-types>
<result-type name="tiles"
class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="login" class="it.actions.cosenonjaviste.LoginAction">
<result name="success" type="tiles">welcome.tiles</result>
<result name="error">Login.jsp</result>
</action>
</package>
</struts>

tiles.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="baseLayout" template="BaseLayout.jsp">
<put-attribute name="title" value="" />
<put-attribute name="header" value="/Header.jsp" />
<put-attribute name="menu" value="/Menu.jsp"/>
<put-attribute name="body" value=""/>
<put-attribute name="footer" value="/Footer.jsp"/>
</definition>
<definition name="welcome.tiles" extends="baseLayout">
<put-attribute name="title" value="Welcome" />
<put-attribute name="body" value="/first/Welcome.jsp" />
</definition>
<definition name="customer.tiles" extends="baseLayout">
<put-attribute name="title" value="Customer Form" />
<put-attribute name="body" value="/Customer.jsp" />
</definition>
<definition name="customer.success.tiles" extends="baseLayout">
<put-attribute name="title" value="Customer Added" />
<put-attribute name="body" value="/SuccessCustomer.jsp" />
</definition>
<definition name="customer.success.tiles" extends="baseLayout">
<put-attribute name="title" value="Customer Added" />
<put-attribute name="body" value="/SuccessCustomer.jsp" />
</definition>
<definition name="file.load.tiles" extends="baseLayout">
<put-attribute name="title" value="Load file" />
<put-attribute name="body" value="/LoadFiles.jsp" />
</definition>
<definition name="file.success.load.tiles" extends="baseLayout">
<put-attribute name="title" value="Dati file selezionato" />
<put-attribute name="body" value="/SuccessLoad.jsp" />
</definition>
</tiles-definitions>

La struttura del progetto è visibile in allegato.

P.s. prima di aggiungere il namespace="first" funzionava tutto correttamente, quindi deve esserci qualche problema nel file tiles.xml o nel struts-login.xml, ma non capisco dove posso aver sbagliato.

webOnEclipse.jpg
 
  • Like
Reactions: ottofonsuppost
Premessa:
A titolo puramente informativo, segnalo che è possibile trovarsi di fronte ad errori che vengono da Eclipse/Tomcat segnalati come gravi, ma che di grave non hanno nulla: per esempio per la classe org.apache.struts2.tiles.StrutsTilesListener mi veniva segnalato un errore di configurazione a runtime, anche se il progetto funzionava correttamente. La cosa ha cominciato a diventarmi sospetta, quando ho riscontrato il medesimo errore anche in quei progetti che non avevano nulla a che fare nè con tiles nè struts.

Un ultima cosa: dal momento che ho notato che con Eclipse risulta alle volte difficoltoso ripristinare una condizione funzionante, anche avendo a disposizione i sorgenti corretti, consiglio vivamente, a consolidamento di uno step, di esportare il progetto completo, in modo che poi non vi siano sorprese.

Ecco quindi il risultato dei miei test:

web.xml
Codice:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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>Archetype Created Web Application</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>
    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener> 
    <context-param>
        <param-name>tilesDefinition</param-name>
        <param-value>/WEB-INF/tiles.xml</param-value>
    </context-param>
    <welcome-file-list>
        <welcome-file>first/Login.jsp</welcome-file>
    </welcome-file-list>
</web-app>


struts-login.xml
Codice:
<?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.custom.i18n.resources" value="ApplicationResources" />
	<package name="Default" extends="struts-default" namespace="/">
		<result-types>
			<result-type name="tiles"
				class="org.apache.struts2.views.tiles.TilesResult" />
		</result-types>
		<action name="login" class="it.actions.cosenonjaviste.LoginAction">
			<result name="success" type="tiles">welcome.tiles</result>
			<result name="error">Login.jsp</result>
		</action>
	</package>
	<package name="Default1" extends="struts-default" namespace="/first">
		<result-types>
			<result-type name="tiles"
				class="org.apache.struts2.views.tiles.TilesResult" />
		</result-types>
		<action name="login" class="it.actions.cosenonjaviste.LoginAction">
			<result name="success" type="tiles">first.welcome.tiles</result>
			<result name="error">Login.jsp</result>
		</action>
	</package>

Qui riporto per semplicità solo quella parte di codice che dovrebbe subire variazioni a seguito di un eventuale modifica al namespace: da notare che per il funzionamento del codice il cambiamento riguarderebbe solo l'attributo "name".

tiles.xml
Codice:
<?xml version="1.0" encoding="UTF-8" ?>
 
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
    <definition name="baseLayout" template="/BaseLayout.jsp">
        <put-attribute name="title" value="Welcome" />
        <put-attribute name="header" value="" />
    </definition>
    <definition name="welcome.tiles" extends="baseLayout">
        <put-attribute name="title" value="Welcome" />
        <put-attribute name="header" value="/Welcome.jsp" />
    </definition>    
    <definition name="first.welcome.tiles" extends="baseLayout">
        <put-attribute name="title" value="Welcome" />
        <put-attribute name="header" value="/first/Welcome.jsp" />
    </definition>    
</tiles-definitions>

Segue il codice che effettua la chiamata alla action. Da notare che nel tag "welcome files" del file web.xml viene chiamato direttamente il login.jsp presente nella directory "first" qui di seguito riportato.

Login.jsp
Codice:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 - Login Application | ViralPatel.net</title>
</head>
 
<body>
<h2>Struts 2 - Login Application</h2>

<s:actionerror />
<s:form action="first/login" >
    <s:textfield name="username" key="label.username" size="20" />
    <s:password name="password" key="label.password" size="20" />
    <s:submit method="execute" key="label.login" align="center" />
</s:form>
</body>
</html>

La struttura della directory dovrebbe quindi essere:

tiles_namespace.gif


Anche se io ho attinto informazioni da più parti, per chi fosse interessato all'argomento può comunque dare un occhiata al seguente link.

http://viralpatel.net/blogs/struts-2-tiles-plugin-tutorial-with-example-in-eclipse/
 
  • Like
Reactions: ottofonsuppost