同<JETTY嵌入式Web容器的开发(一)>,仍旧采用
jetty-hightide-8.1.4.v20120524.zip
本文采用struts2.1.8和spring 2.5.6
建项目步骤同<JETTY嵌入式Web容器的开发(一)>,不做赘述
建成的项目的结构如下:
同<JETTY嵌入式Web容器的开发(一)>,不做赘述
Service类同<JETTY嵌入式Web容器的开发(一)> ,不做赘述
package org.jetty.demo;
public class JettyServerStart {
public static void main(String[] args) { JettyCustomServer server = newJettyCustomServer("./jetty/etc/jetty.xml","/testContext"); server.startServer(); } } |
将struts2.1.8和spring 2.5.6的jar包拷贝入项目的buildpath
|
Src目录下建立struts.properties和struts.xml\ Src下建立目录/spring,里面建立applicationContext.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" version="3.0"> <display-name></display-name>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/applicationContext*.xml</param-value> </context-param>
<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.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener>
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
/spring/applicationContext.xml
<?xml version="1.0" encoding="GBK"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName" default-lazy-init="false">
<context:annotation-config/>
<context:component-scan base-package="cn.com.test"> </context:component-scan>
</beans> |
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> <package name="default" extends="struts-default"> </package> <!-- Add packages here -->
<!-- 测试界面 --> <include file="cn/com/test/action/struts-admin-action.xml"/> <constant name="struts.action.extension" value="do,action"/> </struts> |
cn/com/test/action/struts-admin-action.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>
<package name="test" namespace="/test" extends="default">
<action name="test" class="testAction"> <result name="success">/jsp/success.jsp</result> </action> </package>
</struts> |
package cn.com.test.action;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.ServletActionContext; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
@Controller("testAction") @Scope("prototype") public class TestAction extends ActionSupport {
private static final long serialVersionUID = 9026533527322851032L;
private static Log logger = LogFactory.getLog(TestAction.class);
public String execute() {
ServletActionContext.getRequest().setAttribute("message", "struts,spring测试"); return SUCCESS; } } |
/webRoot/jsp/success.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>Insert title here</title> </head> <body> success!!! ${message} </body> </html> |
执行JettyServerStart |
2012-06-27 17:14:10.859:INFO:oejs.Server:jetty-8.1.4.v20120524 2012-06-27 17:14:11.250:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/testContext,file:/D:/RND/javaathome/eclipsehelio/testjetty/webRoot/} 2012-06-27 17:14:11.250:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/testContext,file:/D:/RND/javaathome/eclipsehelio/testjetty/webRoot/} 2012-06-27 17:14:11.375:INFO:/testContext:Initializing Spring root WebApplicationContext 2012-06-27 17:14:11,375 INFO ContextLoader.initWebApplicationContext(189)-RootWebApplicationContext: initialization started 2012-06-27 17:14:11,406 INFO XmlWebApplicationContext.prepareRefresh(411)-Refreshing org.springframework.web.context.support.XmlWebApplicationContext@39ab89: display name [Root WebApplicationContext]; startup date [Wed Jun 27 17:14:11 CST 2012]; root of context hierarchy 2012-06-27 17:14:11,484 INFO XmlBeanDefinitionReader.loadBeanDefinitions(323)-Loading XML bean definitions from file [D:\RND\javaathome\eclipsehelio\testjetty\bin\spring\applicationContext.xml] 2012-06-27 17:14:11,734 INFO XmlWebApplicationContext.obtainFreshBeanFactory(426)-Bean factory for application context [org.springframework.web.context.support.XmlWebApplicationContext@39ab89]: org.springframework.beans.factory.support.DefaultListableBeanFactory@419d05 2012-06-27 17:14:11,843 INFO DefaultListableBeanFactory.preInstantiateSingletons(414)-Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@419d05: defining beans [org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,testAction]; root of factory hierarchy 2012-06-27 17:14:11,843 INFO ContextLoader.initWebApplicationContext(209)-Root WebApplicationContext: initialization completed in 468 ms 2012-06-27 17:14:11.843:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/testContext,file:/D:/RND/javaathome/eclipsehelio/testjetty/webRoot/} 2012-06-27 17:14:11.843:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/testContext,file:/D:/RND/javaathome/eclipsehelio/testjetty/webRoot/} 2012-06-27 17:14:12,031 INFO XmlConfigurationProvider.info(31)-Parsing configuration file [struts-default.xml] 2012-06-27 17:14:12,187 INFO XmlConfigurationProvider.info(31)-Parsing configuration file [struts-plugin.xml] 2012-06-27 17:14:12,203 INFO XmlConfigurationProvider.info(31)-Parsing configuration file [struts.xml] 2012-06-27 17:14:12,218 INFO BeanSelectionProvider.info(31)-Choosing bean (spring) for class com.opensymphony.xwork2.ObjectFactory 2012-06-27 17:14:12,218 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for class com.opensymphony.xwork2.conversion.impl.XWorkConverter 2012-06-27 17:14:12,218 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for interface com.opensymphony.xwork2.TextProvider 2012-06-27 17:14:12,218 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for interface com.opensymphony.xwork2.ActionProxyFactory 2012-06-27 17:14:12,218 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for interface com.opensymphony.xwork2.conversion.ObjectTypeDeterminer 2012-06-27 17:14:12,218 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for interface org.apache.struts2.dispatcher.mapper.ActionMapper 2012-06-27 17:14:12,218 INFO BeanSelectionProvider.info(31)-Choosing bean (jakarta) for interface org.apache.struts2.dispatcher.multipart.MultiPartRequest 2012-06-27 17:14:12,218 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for class org.apache.struts2.views.freemarker.FreemarkerManager 2012-06-27 17:14:12,218DEBUG BeanSelectionProvider.debug(57)-Choosing bean (struts) for class org.apache.struts2.views.velocity.VelocityManager to be loaded from the ObjectFactory 2012-06-27 17:14:12,218 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for interface org.apache.struts2.components.UrlRenderer 2012-06-27 17:14:12,218 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for interface com.opensymphony.xwork2.validator.ActionValidatorManager 2012-06-27 17:14:12,218 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for interface com.opensymphony.xwork2.util.ValueStackFactory 2012-06-27 17:14:12,218 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for interface com.opensymphony.xwork2.util.reflection.ReflectionProvider 2012-06-27 17:14:12,218 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for interface com.opensymphony.xwork2.util.reflection.ReflectionContextFactory 2012-06-27 17:14:12,234 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for interface com.opensymphony.xwork2.util.PatternMatcher 2012-06-27 17:14:12,234 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for interface org.apache.struts2.dispatcher.StaticContentLoader 2012-06-27 17:14:12,234 INFO BeanSelectionProvider.info(31)-Choosing bean (struts) for interface com.opensymphony.xwork2.UnknownHandlerManager 2012-06-27 17:14:12,234 INFO BeanSelectionProvider.info(31)-Loading global messages from com.demo.struts2.resources.ApplicationResources 2012-06-27 17:14:12,359 INFO StrutsSpringObjectFactory.info(31)-Initializing Struts-Spring integration... 2012-06-27 17:14:12,375 INFO SpringObjectFactory.info(31)-Setting autowire strategy to name 2012-06-27 17:14:12,375 INFO StrutsSpringObjectFactory.info(31)-... initialized Struts-Spring integration successfully 2012-06-27 17:14:13.265:INFO:oejs.AbstractConnector:Started [email protected]:8088 current thread:30| idle thread:26 |
测试访问http://localhost:8088/testContext/test/test.do