Maven 淺談(三)-plugin
在前面的第二部分搭了個spring的架子,把spring-webmvc需要的東西都準備好了
先來把這個web app補充一下吧,就是一個hello world
首先修改 src/main/webapp/WEB-INF目錄下的 web.xml:
除了servlet之外,web.xml里還有一些參數:
contextConfigLocation定義了spring web的配置文件,而listener則表示由spring的ContextLoaderListener來加載這個配置文件
my-servlet.xml則如下例:
這里定義了一個spring的view resolver,這個view resolver就是按照MVC的框架,來找到對應的view的。InternalResourceViewResolver將會根據URL來決定顯示哪個jsp。這些jsp文件都在 WEB-INF目錄下。
maven 在建項目的時候,在src/main/webapp下自動生成了一個index.jsp,可以把它移到 WEB-INF目錄下
下一步,再打開 pom.xml,把這段加進去:
好了,再build一次
mvn clean install
然后運行命令
mvn jetty:run
就是啟動一個jetty,并且把本項目deploy。啟動完成后,就可以用瀏覽器打開
http://localhost:8080/my-webapp/my/
先來把這個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的,版本號不對,以后會有問題<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>
除了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>
<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.jspmaven 在建項目的時候,在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。<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>
好了,再build一次
mvn clean install
然后運行命令
mvn jetty:run
就是啟動一個jetty,并且把本項目deploy。啟動完成后,就可以用瀏覽器打開
http://localhost:8080/my-webapp/my/