码迷,mamicode.com
首页 > 编程语言 > 详细

我的Springmvc开发详细步骤

时间:2016-09-15 19:20:07      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

项目开始前的一些准备

  • step1 新建web project
    技术分享
    技术分享
  • step2 新建用户库,后面会使用
    技术分享
    技术分享
    注意:项目里面不仅需要spring的包,还必须导入commons-logging-jar包,不然项目运行会报错
    技术分享
    技术分享
    结果如下:
    技术分享
  • step3 为项目添加spring容器
    技术分享
    next
    请注意,这里有一个 No jar/zip file contained in selected libraries的提醒,我还没有找到原因,但是只需要再手动的将下载的jar包导入到lib文件夹下,就可以继续后面的步骤
    技术分享
    next
    技术分享
    你会发现项目已经有了spring的容器,项目的目录结构如下
    技术分享
  • step4 配置web.xml文件
    在web.xml中添加如下代码
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
 </servlet-mapping>
  • step5 在src目录下添加mvc.xml文件(这是应为在web.xml中配置了classpath:mvc.xml)
    classpath:即src的路径
    技术分享
    next:
    技术分享
    next:
    技术分享
    next:
    技术分享
    你将得到mvc.xml文件,如下
    技术分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">


</beans>
  • step6 配置mvc.xml
<!-- 配置渲染器 -->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/>
        <!-- 结果视图的前缀 -->
        <property name="prefix" value="/WEB-INF/jsp"/>
        <!-- 结果视图的后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 配置扫描器需要扫描的包 -->
    <context:component-scan base-package="com.wen.controller"/>
  • step7 编写controller
    技术分享
    HelloController.java代码如下:
@Controller
@RequestMapping
public class HelloController {
    @RequestMapping("/hello")
    public ModelAndView hello(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mav = new ModelAndView();
        //视图名称
        mav.setViewName("hello");
        //封装要显示到视图中的数据
        mav.addObject("hello", "hello");
        return mav;
    }
}

hello.jsp代码如下:

 <body>
    获得的数据为:${hello }
  </body>
  • step8

我的Springmvc开发详细步骤

标签:

原文地址:http://blog.csdn.net/zwx2445205419/article/details/52529661

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!