springMVC3学习(一)--框架搭建

本人是基于3.1.1版本开发,如遇jar包版本冲突等其他问题,概不负责。

一:新建web工程,spring001

忽略

二:添加springframework依赖jar包

将dist下的jar全部加进去 然后另加一个commons-logging.jar

三:修改web.xml

[html] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. <servlet>
  2. <servlet-name>springMVC</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <init-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>/WEB-INF/config/springMVC.xml</param-value>
  7. </init-param>
  8. <load-on-startup>1</load-on-startup>
  9. </servlet>
  10. <servlet-mapping>
  11. <servlet-name>springMVC</servlet-name>
  12. <url-pattern>/</url-pattern>
  13. </servlet-mapping>

默认DispatcherServlet会加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件,

本示例为WEB-INF/springMVC-servlet.xml

默认路径为WEB-INF下,也可修改为如下配置

[html] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. <init-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <!--<param-value>classpath:conf/springMVC.xml</param-value>-->
  4. <!--<param-value>classpath:/conf/springMVC.xml</param-value>-->
  5. <!--<param-value>classpath*:/conf/springMVC.xml</param-value>-->
  6. <!--以上三种形式也可以-->
  7. <param-value>classpath:conf/springMVC.xml</param-value>
  8. </init-param>
四:添加springMVC配置文件

[html] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. default-lazy-init="true"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  12. http://www.springframework.org/schema/mvc
  13. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  14. ">
  15. <!--默认扫描的包路径-->
  16. <context:component-scanbase-package="com.itmyhome"/>
  17. <!--添加注解驱动-->
  18. <mvc:annotation-driven/>
  19. <!--定义跳转的文件的前后缀-->
  20. <beanid="viewResolver"
  21. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  22. <propertyname="prefix"value="/"/><!--默认放在WebRoot下-->
  23. <propertyname="suffix"value=".jsp"/>
  24. </bean>
  25. </beans>
PS:<context:component-scan base-package="com.itmyhome" />

base-package后的内容只是包路径,千万不能把类名也写出来 比如com.itmyhome.Login

否则会报如下错误

警告: No mapping found for HTTP request with URI [/spring001/login] in DispatcherServlet with name 'springMVC'


五:后台Action

[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. packagecom.itmyhome;
  2. importorg.springframework.stereotype.Controller;
  3. importorg.springframework.web.bind.annotation.RequestMapping;
  4. @Controller
  5. publicclassLogin{
  6. @RequestMapping(value="login")
  7. publicStringlogin(){
  8. System.out.println("*******已经进入*********");
  9. return"welcome";//即返回welcome.jsp
  10. }
  11. }

六:返回JSP

新建welcome.jsp 里面随便写点内容 如:Hello springMVC!

最后在浏览器中输入地址 验证 如图:


工程目录如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值