2. Configure Struts2
(1) web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/org/xqtu/samples/bean/samples-common.xml,
/WEB-INF/classes/org/xqtu/samples/bean/samples-jndi.xml,
/WEB-INF/classes/org/xqtu/samples/bean/samples-dao-hibernate.xml,
/WEB-INF/classes/org/xqtu/samples/bean/samples-services.xml,
/WEB-INF/classes/org/xqtu/samples/bean/samples-actions.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
l Struts2 use FilterDispatcher filter to inspect the incoming requests looking for requests target to Struts2 actions. And url pattern “/*” means the filter will inspect all requests.
l To create Spring container, we use Spring ContextLoaderListener listener. And pass in parameter contextConfigLocation to the listener to specify appropriate locations.
(2) struts.xml(located WEB-INF/classes/)
<?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="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="LoginInput">
<result>/WEB-INF/jsp/login.jsp</result>
</action>
<action name="Login" class="loginAction">
<result>/WEB-INF/jsp/success.jsp</result>
<result name="input">/WEB-INF/jsp/login.jsp</result>
</action>
</package>
</struts>
l The entry point into the Struts2 XML declarative architecture is the struts.xml file. It can include other XML documents, allowing for modularization.
l The struts element is the mandatory document root of all Struts2 XML files.
l The package element declares a namespace that’ll be used when the framework maps URLs to these actions. For instance,
http://localhost:8080/Struts2Spring/Login.action
Ø The URL combines the servlet context(/Struts2Spring) with the package namespace(/) and the action name (Login.action). Note that the action name takes the .action extension.
l The action element declares the action mapping with a logic name.
Ø Use empty action components to forward to your results, even if they’re simple JSPs that require no dynamic processing, just like LoginInput action.
Ø If you needs a really action to process business logic, you can use class attribute to specify action class.
Ø For Spring integration, the class attribute specify Spring bean ID, just like Login action.
l Each result element names a JSP page that it will use to render the result page. If the name attribute is omitted, use default “success”
(3) samples-action.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- Struts Action config -->
<bean name="loginAction"
class="org.xqtu.samples.web.action.LoginAction">
<property name="userService" ref="userService" />
</bean>
</beans>
l Here we change the bean ID to consist with the class attribute in the action mapping.