springMVC-router建立一個簡單的web工程
1、安裝jdk和tomcat,安裝方法不再贅述,此處分別是jdk1.7.0_79和tomcat 7.0.27;
2、在eclipse中新建一個web工程:
定義一個工程名和工程的工作空間,並選擇相應的tomcat,此處命名為story,空間為workspace:
下一步,編譯好的class的預設輸出目錄,先預設,之後可以更改;
下一步,各項配置資訊和依賴庫等的位置,使用預設的目錄名WebContent,並生成預設的描述檔案web.xml方便以後配置;
新建工程完成後,生成的class可以更改到WEB-INF目錄下:
3、生成的目錄結構如下所示,但需要把工程以來的jar包匯入,jar包都匯入到lib目錄下即可:
Spring MVC所需要的jar包下載地址:
我們此處使用4.3.3-Realease版本;
此外我們使用Spring MVC router的方法通過router呼叫java bean controller的方法,簡化開發過程,其依賴jar包可從https://github.com/resthub/Springmvc-router/下載maven工程後用eclipse打成jar包使用;
此外所需要的slf4j-api.jar、org.apache.log4j.jar、jregex.jar、commons-logging.jar、commons-io.jar均可以從網上直接下載,不再贅述;
下載完成後將所需要的jar包匯入到lib目錄下,並將所有jar包配置到編譯路徑下以便引用:
4、向web.xml中新增用於前端分發請求的DispatcherServlet,命名為storyDispatchServlet,其所需的配置引數在contextConfigLocation 中配置路徑,在story-servlet.xml 中具體配置:
<servlet> <servlet-name>storyDispatchServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/story-servlet.xml</param-value> </init-param> <load-on-startup>9</load-on-startup> </servlet> <servlet-mapping> <servlet-name>storyDispatchServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
其中url-pattern 配置為/*即表示所有從/story/過來的請求都由該DispatcherServlet處理分發;
5、配置story-servlet.xml
根據web.xml配置DispatcherServlet的配置檔案路徑WEB-INF/story-servlet.xml新建配置檔案,根據https://github.com/resthub/Springmvc-router/的指導,將router相關的配置資訊新增到story-servlet.xml檔案中,並且需要在該檔案中新增spring MVC所需的配置資訊,story-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: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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--
Enable bean declaration by annotations, update base package according to your project
-->
<context:annotation-config />
<mvc:annotation-driven />
<!--
Package to scan for Controllers.
All Controllers with @Controller annotation are loaded as such.
-->
<context:component-scan base-package="com.feyn.story.controllers" />
<!--
Choose HandlerMapping.
RouterHandlerMapping loads routes configuration from a file.
Router adapted from Play! Framework.
@see http://www.playframework.org/documentation/1.2.4/routes#syntax
for route configuration syntax.
Example:
GET /home PageController.showPage(id:'home')
GET /page/{id} PageController.showPage
-->
<bean id="handlerMapping"
class="org.resthub.web.springmvc.router.RouterHandlerMapping">
<property name="routeFiles">
<list>
<value>classpath:storyconfig/story-routes.conf</value>
<!--
Router will *append* routes declared in additional files
<value>addroutes.conf</value>
-->
</list>
</property>
<!--
Uncomment the following configuration line
if you want routes to be dynamically reloaded when
route files are modified.
Can be a good idea in dev mode, not so much in production!
-->
<property name="autoReloadEnabled" value="true" />
</bean>
</beans>
其中:
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
這兩項是mvc所需的配置項,此外還需要新增<mvc:annotation-driven/>配置項註冊HandlerMapping與HandlerAdapter兩個bean,保證DispatcherServlet可以找到控制器並把請求分發給控制器;
所有的用於業務控制的java bean都由@Controller註解來標識,並在story-servlet.xml中用掃描方式注入:
<context:component-scanbase-package="com.feyn.story.controllers"/>
並在handlerMapping中配置路由的對應關係表story-routes.conf,該路由配置表維護一個請求和控制器的對應關係;
6、然後根據xml中配置檔案中的配置新建包和conf,包路徑按照story-servlet.xml來定義:
HelloController.java程式碼:
package com.feyn.story.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class HelloController {
public @ResponseBody String sayHelloTo(@PathVariable(value = "name") String name)
{
return "Hello " + name;
}
}
story-routes.conf配置對應表如下:
GET /hello/{<[a-zA-Z]+>name} HelloController.sayHelloTo
上述controller程式碼和routes.conf配置方法同樣參考https://github.com/resthub/Springmvc-router/;
7、配置server
單擊server並新建tomcat伺服器:
下一步,將剛剛完成的web工程新增到伺服器中:
對伺服器進行配置,雙擊該伺服器,將應用部署位置等修改到webapps目錄下:
8、啟動該服務,就可以在瀏覽器或者postman中傳送請求,並看到返回:
或者