先來(lái)把這個(gè)web app補(bǔ)充一下吧,就是一個(gè)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的,版本號(hào)不對(duì),以后會(huì)有問(wèn)題<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里還有一些參數(shù):
contextConfigLocation定義了spring web的配置文件,而listener則表示由spring的ContextLoaderListener來(lái)加載這個(gè)配置文件
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>
這里定義了一個(gè)spring的view resolver,這個(gè)view resolver就是按照MVC的框架,來(lái)找到對(duì)應(yīng)的view的。InternalResourceViewResolver將會(huì)根據(jù)URL來(lái)決定顯示哪個(gè)jsp。這些jsp文件都在 WEB-INF目錄下。
<mvc:view-controller path="/" view-name="index"/>
則是直接把根指向了index,通過(guò)view resolver,它其實(shí)是指向了 WEB-INF/index.jspmaven 在建項(xiàng)目的時(shí)候,在src/main/webapp下自動(dòng)生成了一個(gè)index.jsp,可以把它移到 WEB-INF目錄下
下一步,再打開(kāi) pom.xml,把這段加進(jìn)去:
<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>
這里定義了兩個(gè)plug-in,一個(gè)很明顯,意思是用java 1.6版本來(lái)做編譯。而另一個(gè)則是maven的jetty插件,jetty是一個(gè)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
然后運(yùn)行命令
mvn jetty:run
就是啟動(dòng)一個(gè)jetty,并且把本項(xiàng)目deploy。啟動(dòng)完成后,就可以用瀏覽器打開(kāi)
http://localhost:8080/my-webapp/my/