个人学习记录-Spring mvc第一阶段

SpringMVC的搭建,环境为Eclipse


一、创建一个maven工程


创建完成后,项目会出现错误指示,右键Deployment Descriptor..选择Generate Deployment Descriptor stub 生成web.xml文件。


二、导入所需要的jar包


1.tomcat相关

右键项目-->properties-->Targeted Runtimes-->勾选Apache Tomcat-->Apply-->OK 

2.其他所需的包,使用pom.xml文件导入

打开pom.xml文件选择Dependencies->add

搜索spring-webmvc选择org.springframework spring-webmvc选择版本3.2.8 保存

如需测试,可使用相同的方法导入junit包,选择最新版本即可。

检查包是否导入:Java Resources-->Libraries-->Maven Dependencies看下包是否导入。


三、添加spring配置文件:如命名为ac.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:context="http://www.springframework.org/schema/context" 

xmlns:jdbc="http://www.springframework.org/schema/jdbc"  

xmlns:jee="http://www.springframework.org/schema/jee" 

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop" 

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:util="http://www.springframework.org/schema/util"

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans- 3.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring- context-3.2.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa- 1.3.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

</beans>


四、在web.xml里配置DispatcherServlet。


<servlet>

   <servlet-name>springmvc</servlet-name>

   <servlet-class>

   org.springframework.web.servlet.DispatcherServlet

   </servlet-class>

   <!--

   DispatcherServlet的初始化方法在执行时会启动spring容器,

   所以需要通过配置 初始化参数来指定spring配置文件的位置。

    -->

    <init-param>

   <param-name>contextConfigLocation</param-name>

   <param-value>classpath:ac.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>


五、编写Controller类(示例)

在src/main/java下建一个包controller,在此包下建一个类如MyController.java


package controller;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.Controller;

 

public class MyControllerimplements Controller{

 

public ModelAndView handleRequest(HttpServletRequestrequest, HttpServletResponse response) throws Exception {

//测试

System.out.println("MyControlloer's handleRequest()");

/*

  * ModelAndView有两个构造器:

  * ModelAndView(String viewName):viewName是视图名

  * ModelAndView(String viewName,Map data):data是 *处理结果

  */

//返回一个ModelAndView对象

return new ModelAndView("hello");

}

}


六、写JSP。

在WEB-INF下建立jsp文件夹,并在文件夹下建立hello.jsp。由于是示例,写的简单点


<%@page pageEncoding="utf-8" %>

<!DOCTYPE html>

<html>

<head></head>

<body style="font-size:30px;">

Hello,SpringMVC!

</body>

</html>

 

七、配置HandlerMapping和ViewResolver。(在spring的配置文件ac.xml中)


<!-- 配置HandlerMapping -->

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="mappings">

<props>

<prop key="/hello.do">myController</prop>

</props>

</property>

</bean>

<bean id="myController" class="controller.MyController"/>

<!-- 配置视图解析器 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/jsp/" />

<property name="suffix" value=".jsp" />

</bean>


八、 将项目部署到tomcat服务器上进行测试

servers窗口下右键tomcat服务器-->add and remove将你需要的项目add到服务器下-->finish

启动服务器

在浏览器中输入

http://localhost:8080/springmvc-test01/hello.do可看见页面出现,表示已搭建成功(其中springmvc-test01是项目名)

页面显示:

Hello,SpringMVC!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值