Spring MVC工程结构及配置整理

1、首先建一个maven webapp工程,然后pom里面 加上依赖的jar包

1) aopalliance-1.0.jar

2)  commons-logging-1.1.3.jar

3)  spring-aop-3.2.8.RELEASE.jar

4)  spring-beans-3.2.8.RELEASE.jar

5)  spring-context-3.2.8.RELEASE.jar

6)  spring-core-3.2.8.RELEASE.jar

7)  spring-expression-3.2.8.RELEASE.jar

8)  spring-web-3.2.8.RELEASE.jar

9)  spring-webmvc-3.2.8.RELEASE.jar

10)             jstl-1.1.2.jar

2、工程结构


1)application_spring_mvc.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:context="http://www.springframework.org/schema/context"    
  7.     xmlns:mvc="http://www.springframework.org/schema/mvc"    
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  9.     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  10.     http://www.springframework.org/schema/tx   
  11.     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
  12.     http://www.springframework.org/schema/context  
  13.     http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  14.     http://www.springframework.org/schema/mvc  
  15.     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
  16.   
  17.     <!-- 自动扫描的包名 -->  
  18.     <context:component-scan base-package="com.clj"/>  
  19.   
  20.     <!-- 默认的注解映射的支持,自动注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->  
  21.     <mvc:annotation-driven />  
  22.   
  23.     <!-- 视图解释类 -->  
  24.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  25.         <property name="prefix" value="/WEB-INF/jsp/"/>  
  26.         <property name="suffix" value=".jsp"/>  
  27.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
  28.     </bean>  
  29.       
  30.     <!-- 对静态资源文件的访问-->  
  31.     <mvc:resources mapping="/images/**" location="/WEB-INF/images/" cache-period="31556926"/>  
  32.     <mvc:resources mapping="/js/**" location="/WEB-INF/js/" cache-period="31556926"/>  
  33.     <mvc:resources mapping="/css/**" location="/WEB-INF/css/" cache-period="31556926"/>  
  34.   
  35. </beans>   

3、web.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE web-app PUBLIC  
  2.  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  3.  "http://java.sun.com/dtd/web-app_2_3.dtd" >  
  4.   
  5. <web-app>  
  6.   
  7.   <!-- ================spring mvc 适配器================ -->  
  8.     <servlet>  
  9.         <servlet-name>springMVC</servlet-name>  
  10.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  11.         <init-param>  
  12.             <param-name>contextConfigLocation</param-name>  
  13.             <param-value>classpath*:/applicationContext/application_spring_mvc.xml</param-value>  
  14.         </init-param>  
  15.         <load-on-startup>1</load-on-startup>  
  16.     </servlet>  
  17.     <servlet-mapping>  
  18.         <servlet-name>springMVC</servlet-name>  
  19.         <url-pattern>/</url-pattern>  
  20.     </servlet-mapping>  
  21.     
  22.   <!-- ================================================== -->  
  23.     <servlet-mapping>          
  24.         <servlet-name>default</servlet-name>         
  25.         <url-pattern>*.html</url-pattern>        
  26.     </servlet-mapping>     
  27.   
  28.     <welcome-file-list>  
  29.         <welcome-file>index.html</welcome-file>  
  30.     </welcome-file-list>  
  31. </web-app>  

4、index.html

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <html>  
  2. <body>  
  3. <h2>Hello World!</h2>  
  4. <form action="./user/save" method="get">  
  5.     <input id="name" name="name" value="张三"/><br/>  
  6.     <input id="password" name="password" value="123456"/><br/>  
  7.     <input type="submit" value="提交"/>  
  8. </form>  
  9. </body>  
  10. </html>  


5、UserAction.Java

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.clj.test.user.action;  
  2.   
  3. import org.springframework.context.annotation.Scope;  
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import org.springframework.web.bind.annotation.RequestMethod;  
  7. import org.springframework.web.servlet.ModelAndView;  
  8.   
  9. /** 
  10.  * <一句话功能简述> 
  11.  * <功能详细描述> 
  12.  *  
  13.  * @author  Administrator 
  14.  * @version  [版本号, 2014年3月4日] 
  15.  * @see  [相关类/方法] 
  16.  * @since  [产品/模块版本] 
  17.  */  
  18. @Controller  
  19. @Scope("prototype")   
  20. @RequestMapping("/user")  
  21. public class UserAction  
  22. {  
  23.     @RequestMapping(value="/save",method=RequestMethod.GET)  
  24.     public ModelAndView  save(String name,String password)  
  25.     {  
  26.         System.out.println("接收到的用户信息:"+name);  
  27.           
  28.         ModelAndView mov=new ModelAndView();  
  29.         mov.setViewName("/test/saveUserSuccess");  
  30.         mov.addObject("msg""保存成功了");  
  31.           
  32.         return mov;  
  33.     }  
  34. }  

6、saveUserSuccess.jsp

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <%@ 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>添加用户成功</title>  
  7. </head>  
  8. <body>  
  9. <h1>操作成功了</h1>  
  10. 后台返回的信息:${msg}  
  11. </body>  
  12. </html>  

四、发布工程到tomcat进行测试

具体eclipse中如何使用tomcat进行maven  webapp项目测试请参看:

http://blog.csdn.net/clj198606061111/article/details/20221133


1)  浏览器中输入:http://localhost:8080/demoSpringMVC/

便可进入index.html页面,如下图:

2)  提交后


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值