Maven 浅谈(三)-plugin

Maven 浅谈(三)-plugin

在前面的第二部分搭了个spring的架子,把spring-webmvc需要的东西都准备好了
先来把这个web app补充一下吧,就是一个hello world

首先修改 src/main/webapp/WEB-INF目录下的 web.xml:
<? xml version = " 1.0 "  encoding = " UTF-8 " ?>
< web - app version = " 2.5 "  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_2_5.xsd " >
    
< display - name > my Web Application </ display - name >

    
< context - param >
        
< param - name > contextConfigLocation </ param - name >
        
< param - value >/ WEB - INF / my - servlet.xml </ param - value >
    
</ context - param >

    
< listener >
        
< listener - class > org.springframework.web.context.ContextLoaderListener </ listener - class >
    
</ listener >

    
< servlet >
        
< servlet - name > my </ servlet - name >
        
< servlet - class > org.springframework.web.servlet.DispatcherServlet </ servlet - class >
    
</ servlet >

    
< servlet - mapping >
        
< servlet - name > my </ servlet - name >
        
< url - pattern >/ my /* </url-pattern>
    </servlet-mapping>

</web-app>
这里,web-app的版本是2.5,早期的版本是不支持EL的,版本号不对,以后会有问题
除了servlet之外,web.xml里还有一些参数:
contextConfigLocation定义了spring web的配置文件,而listener则表示由spring的ContextLoaderListener来加载这个配置文件

my-servlet.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 "  
    xmlns:p
= " http://www.springframework.org/schema/p "   
    xmlns:context
= " http://www.springframework.org/schema/context "
    xmlns:mvc
= " http://www.springframework.org/schema/mvc "
    xsi:schemaLocation
= " http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http: // www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http: // www.springframework.org/schema/mvc      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">

    
< bean
        
class = " org.springframework.web.servlet.view.InternalResourceViewResolver " >
        
< property name = " prefix "  value = " /WEB-INF/jsp/ "   />
        
< property name = " suffix "  value = " .jsp "   />
    
</ bean >
    
    
< mvc:view - controller path = " / "  view - name = " index " />
        
</ beans >

这里定义了一个spring的view resolver,这个view resolver就是按照MVC的框架,来找到对应的view的。InternalResourceViewResolver将会根据URL来决定显示哪个jsp。这些jsp文件都在 WEB-INF目录下。

< mvc:view - controller path = " / "  view - name = " index " />
则是直接把根指向了index,通过view resolver,它其实是指向了 WEB-INF/index.jsp

maven 在建项目的时候,在src/main/webapp下自动生成了一个index.jsp,可以把它移到 WEB-INF目录下

下一步,再打开 pom.xml,把这段加进去:
     < build >
        
< finalName > my </ finalName >
        
< plugins >
            
< plugin >
                
< groupId > org.mortbay.jetty </ groupId >
                
< artifactId > maven - jetty - plugin </ artifactId >
                
< version > 6.1 . 26 </ version >
            
</ plugin >
            
< plugin >
                
< artifactId > maven - compiler - plugin </ artifactId >
                
< configuration >
                    
< source > 1.6 </ source >
                    
< target > 1.6 </ target >
                
</ configuration >
            
</ plugin >
        
</ plugins >
    
</ build >
这里定义了两个plug-in,一个很明显,意思是用java 1.6版本来做编译。而另一个则是maven的jetty插件,jetty是一个web server。

好了,再build一次
mvn clean install
然后运行命令
mvn jetty:run
就是启动一个jetty,并且把本项目deploy。启动完成后,就可以用浏览器打开
http://localhost:8080/my-webapp/my/





你可能感兴趣的:(Maven 浅谈(三)-plugin)