flio

   :: 首页 :: 联系 ::  :: 管理
  0 Posts :: 6 Stories :: 6 Comments :: 0 Trackbacks
这段时间开始封装公司内部使用的开发框架,基于性能的考虑,使用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中配置如下代码:
 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

重点只有两句,就是开启注解,和自动扫描控制器
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;
     }

}



这样基本就可以了。发布项目,访问地址-->  http://localhost:8080/项目名/helloworld/hello.test
就可以看到控制台输出helloworld了。
未完待续....

posted on 2012-05-30 13:38 flio 阅读(586) 评论(1)  编辑  收藏 所属分类: 框架技术

Feedback

# re: SpringMVC+hibernate3(1.让springmvc跑起来) 2012-05-31 08:30 x7
哥顶了  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: