這段時間開始封裝公司內部使用的開發框架,基于性能的考慮,使用srpingmvc+hibernate技術,下面是一些搭建過程(不包含dao層封裝),寫下來備忘。
1.讓springmvc跑起來
這個過程很簡單。導入spring的jar包就好了,這里簡單的介紹下jar包吧。主要分srping的實現包,和依賴包
主包:spring-framework-3.1.1.RELEASE-with-docs.zip
依賴:spring-framework-3.0.1.RELEASE-A-dependencies.zip
主包中不需要全部加進去,選一些需要的就好了如下:

然后再加入依賴:

加入了jar包后就開始配置web.xml文件,在web.xml中加入如下代碼。
<!--初始化srpingMVC-->
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 這樣可以配置xml-servlet,不默認找test-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/configSource/test-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<!--這里我是攔截以.test結尾的請求-->
<url-pattern>*.test</url-pattern>
</servlet-mapping>

因為srpingmvc是基于servlet實現的。所有請求都需要通過DispatcherServlet來轉發請求
解釋下init-param這段,默認可以不需要配置的,只要在WEB-INF下面定義test-servlet.xml就可以自己找到的,(test-servlet.xml,這個名字的由來是因為我的servlet的name是test,所有有test-servlet.xml文件就會自動找到這個文件,紅色字體標注),這樣的話下面的init-param這一個節點就可以不需要配置了。
但是為了管理方便,我放在了configSource這個包里面,所以我在param-value中的<param-value>classpath:/configSource/*-servlet.xml</param-value>是這樣配置的。基本這樣就配置完了,可以簡單測試,就來個helloworld例子吧。
在test-servlet.xml中配置如下代碼:
重點只有兩句,就是開啟注解,和自動掃描控制器
base-package="com.Integrat.*.controller" 這個應該懂事這么意思了吧,不解釋。
配置好這些了就開始寫controller吧
代碼如下:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;

@org.springframework.stereotype.Controller //這里是注解,表示這是一個控制器
@RequestMapping("/helloworld") //這里是訪問路徑,個人感覺和struts的namespace一點類似,不過功能不止如此,略過
public class HelloWorldController{
@RequestMapping("/hello") //注意這里也寫個
public String query(){
System.out.println("hello world!!!");
return null;
}
}

1.讓springmvc跑起來
這個過程很簡單。導入spring的jar包就好了,這里簡單的介紹下jar包吧。主要分srping的實現包,和依賴包
主包:spring-framework-3.1.1.RELEASE-with-docs.zip
依賴:spring-framework-3.0.1.RELEASE-A-dependencies.zip
主包中不需要全部加進去,選一些需要的就好了如下:

然后再加入依賴:

加入了jar包后就開始配置web.xml文件,在web.xml中加入如下代碼。

















因為srpingmvc是基于servlet實現的。所有請求都需要通過DispatcherServlet來轉發請求
解釋下init-param這段,默認可以不需要配置的,只要在WEB-INF下面定義test-servlet.xml就可以自己找到的,(test-servlet.xml,這個名字的由來是因為我的servlet的name是test,所有有test-servlet.xml文件就會自動找到這個文件,紅色字體標注),這樣的話下面的init-param這一個節點就可以不需要配置了。
但是為了管理方便,我放在了configSource這個包里面,所以我在param-value中的<param-value>classpath:/configSource/*-servlet.xml</param-value>是這樣配置的。基本這樣就配置完了,可以簡單測試,就來個helloworld例子吧。
在test-servlet.xml中配置如下代碼:
1
<beans xmlns="http://www.springframework.org/schema/beans"
2
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
3
xmlns:context="http://www.springframework.org/schema/context"
4
xmlns:mvc="http://www.springframework.org/schema/mvc"
5
xsi:schemaLocation="http://www.springframework.org/schema/beans
6
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7
http://www.springframework.org/schema/mvc
8
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
9
http://www.springframework.org/schema/context
10
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
11
<!-- 自動掃描com.baobaotao.web 包下的@Controller標注的類控制器類 -->
12
<context:component-scan base-package="com.Integrat.*.controller" />
13
<!-- 啟動Spring MVC的注解功能,完成請求和注解POJO的映射 -->
14
<mvc:annotation-driven/>
15
</beans>
16

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

重點只有兩句,就是開啟注解,和自動掃描控制器
base-package="com.Integrat.*.controller" 這個應該懂事這么意思了吧,不解釋。
配置好這些了就開始寫controller吧
代碼如下:
















這樣基本就可以了。發布項目,訪問地址--> http://localhost:8080/項目名/helloworld/hello.test
就可以看到控制臺輸出helloworld了。
未完待續....