spring3mvc注解版


开发环境如下:

OS:windows

IDE:Eclipse3.7.2

第一步,新建Web工程:


第二步,添加springframework依赖jar包:


其中commons-logging-1.1.1.jar是spring日志依赖jar包,jstl-api-1.2.jar与jstl-impl-1.2.jar是Oracle官方的JSTL接口与实现,如果选Apche的也可以。

第三步,修改web.xml,实现spring的控制:

[html]  view plain copy
  1. <span style="font-family:Microsoft YaHei;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  3.     <display-name>Spring3mvc</display-name>  
  4.     <!-- 添加spring3控制器及映射规则 -->  
  5.     <servlet>  
  6.         <servlet-name>spring3mvc</servlet-name>  
  7.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  8.         <load-on-startup>1</load-on-startup>  
  9.     </servlet>  
  10.     <servlet-mapping>  
  11.         <servlet-name>spring3mvc</servlet-name>  
  12.         <url-pattern>/app/*</url-pattern>  
  13.     </servlet-mapping>  
  14.     <!-- 默认跳转到根目录下的页面,在这里为index.jsp -->  
  15.     <welcome-file-list>  
  16.         <welcome-file>index.jsp</welcome-file>  
  17.     </welcome-file-list>  
  18. </web-app></span>  

第四步,添加spring配置文件,其文件名需与web.xml中的<servlet-name></servlet-name>中的名字一致:

[html]  view plain copy
  1. <span style="font-family:Microsoft YaHei;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <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="  
  3.     http://www.springframework.org/schema/beans   
  4.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  5.     http://www.springframework.org/schema/context  
  6.     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  7.     http://www.springframework.org/schema/mvc  
  8.     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
  9.     ">  
  10.     <!-- 默认扫描的包路径 -->  
  11.     <context:component-scan base-package="cn.org.spartacus.spring" />  
  12.     <!-- 添加注解驱动 -->  
  13.     <mvc:annotation-driven />  
  14.     <!-- 定义跳转的文件的前后缀 -->  
  15.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  16.         <property name="prefix" value="/WEB-INF/jsp/" />  
  17.         <property name="suffix" value=".jsp" />  
  18.     </bean>  
  19. </beans></span>  

第五步,完成前后台代码:

首先,在WEB-INF下新建一个jsp包,与上文中prefix的值相对应;并在包内新建一jsp文件,名字随便,在这里我叫它success.jsp。

[html]  view plain copy
  1. <span style="font-family:Microsoft YaHei;font-size:18px;"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6. <title>hello world</title>  
  7. </head>  
  8. <body>  
  9.     Hello world!  
  10. </body>  
  11. </html></span>  

其次,编写后台代码,包路径一般是公司域名倒写,我这里随便写了,如下:

[java]  view plain copy
  1. <span style="font-family:Microsoft YaHei;font-size:18px;">/**   
  2.  * 项目名称:Spring3mvc demo 
  3.  * Copyright © 2010-2012 spartacus.org.cn All Rights Reserved 
  4.  */    
  5. package cn.org.spartacus.spring;    
  6.   
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9.   
  10. /** 
  11.  * Description: TODO 
  12.  * @author hankaibo 
  13.  * @date 2012-11-4 
  14.  * @version v1.0 
  15.  */  
  16. @Controller  
  17. @RequestMapping("index")  
  18. public class IndexController {  
  19.     @RequestMapping("helloWorld")  
  20.     public String helloworld() {  
  21.         // return "success"; //跳转到success页面  
  22.         return "index";  
  23.     }  
  24.   
  25. }  
  26. </span>  

第六步,验证。

首先编写根目录下的index.jsp页面,并在其内实现跳转,文件内容如下:

[html]  view plain copy
  1. <span style="font-family:Microsoft YaHei;font-size:18px;"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>index</title>  
  8. </head>  
  9. <body>  
  10.     OK!  
  11.     <c:redirect url="/app/index/helloWorld" /><!-- 用来实现跳转,验证spring成功与否 -->  
  12. </body>  
  13. </html></span>  
最后,打开浏览器,输入地址,验证。



测试成功,^_^.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值