MAVEN 搭建基于springmvc web项目+jetty-maven-plugin测试

来源:互联网 发布:淘宝网店如何引流量 编辑:程序博客网 时间:2024/06/06 01:04

部署参考此配置过程:点击打开链接

初次在工作中使用spring mvc + maven, 以下记录基于这些工具开发的流程,遇到的问题。


原理介绍:



服务器初始化:

1. initWebApplicationContext

2. 初始化org.springframework.beans.factory.support.DefaultListableBeanFactory bean工厂

3.  初始化dispatcherServlet->load servletContext资源文件(即controller的配置文件spring-web-servlet)

4. 初始化bean list,注册handler(Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2b2057: defining beans [generalController 我的controller, org.springframework.context.annotation.internalConfigurationAnnotationProcessor  spring用到的bean     。。。。])

5. 初始化bean(Mapped "{[/index],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.grassland.GeneralController.index())


原理:

request: localhost:8080/index-> 

1. dispatcherServlet 根据请求地址派个某controller处理 

2. An appropriate handler is searched for. If a handler is found, the execution chain associated with the handler (preprocessors, postprocessors, and controllers) is executed in order to prepare a model or rendering,把必要数据通过model返回 (model.addAttribute("liming", "黎明你好");  ), 同时返回一个逻辑地址

3.  internalResourceViewResolver  (即view Template ) 通过逻辑名成和prefix和suffix组合,形成实际的路径。如返回的逻辑字符串为index, 组合后的路径为 /WEB-INF/jsp/index.jsp 返回此view路径  

4. dispatcherServlet返回response


遇到的问题:

1. spring web controller的配置注意点,组件扫描包要配置正确,否则无法扫描controller

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">    <context:component-scan base-package="com.grassland" /><mvc:annotation-driven /><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="warnLogCategory" value="warn"/></bean> </beans>

使用jetty测试配置: 

JETTY如何启动:http://iammr.7.blog.163.com/blog/static/4910269920123131119515/

<plugin><groupId>org.mortbay.jetty</groupId><artifactId>maven-jetty-plugin</artifactId><version>6.1.24</version><configuration><stopPort>9966</stopPort><stopKey>foo</stopKey><contextPath>/</contextPath><scanIntervalSeconds>3</scanIntervalSeconds><scanTargetPatterns><scanTargetPattern><directory>src/main/webapp/WEB-INF</directory><excludes><exclude>**/*.jsp</exclude></excludes><includes><include>**/*.properties</include><include>**/*.xml</include></includes></scanTargetPattern></scanTargetPatterns><requestLog implementation="org.mortbay.jetty.NCSARequestLog"><filename>target/yyyy_mm_dd.request.log</filename><retainDays>90</retainDays><append>true</append><extended>false</extended><logTimeZone>GMT</logTimeZone></requestLog><connectors><connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"><port>8080</port><maxIdleTime>60000</maxIdleTime></connector></connectors></configuration></plugin>


jetty调试配置:http://www.blogjava.net/alwayscy/archive/2007/06/01/118584.html  (此调试方法利用远程调试原理可参考https://www.ibm.com/developerworks/cn/opensource/os-eclipse-javadebug/#resources)