SpringMVC初解
一、插件下载:
方法一:在菜单项中选择help---->>Install New Software---->>在Install页面选择add----->>在location中输入:http://dist.springsource.com/release/TOOLS/update/e4.5/(其中4.5表示eclipse版本号)
查看eclipse版本号方法:在菜单项中选择help------>>Aboat Eclipse 或者在eclipse目录中找到readme_eclipse.html
笔者目录为D:\eclipse\readme\readme_eclipse.html
方法二:在菜单项中选择help---->>Eclipse Marketplace中搜索SpringMVC 然后直接点击Install即可 等待安装 安装完成后重启eclipse即可。
二、导入相关包:
–commons-logging-1.1.3.jar
–spring-aop-4.0.0.RELEASE.jar
–spring-beans-4.0.0.RELEASE.jar
–spring-context-4.0.0.RELEASE.jar
–spring-core-4.0.0.RELEASE.jar
–spring-expression-4.0.0.RELEASE.jar
–spring-web-4.0.0.RELEASE.jar
–spring-webmvc-4.0.0.RELEASE.jar
相关包笔者已经分享到百度云盘了,需要的朋友可以在此下载:
链接地址:http://pan.baidu.com/s/1pLkH9KR
密码:xz8z
三、项目的创建
3.1:web-xml的配置:
选择Alt+/选择dispatherservlet然后按enter健即可自动生成以下内容
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:DoubleKill.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
然后配置param-value 将其设置成classpath:xxx.xml
xxx代表的是新建的Spring Bean Configuration File文件的文件名
然后设置:url-pattern其中/表示拦截所有,也可设置相应想拦截的文件名
3.2创建Spring Bean Configuration File:
创建时勾选bean context mvc 选项
创建成功后会自动生成如下代码:
<?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: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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> </beans>
须在</beans>标签上 添加
<context:component-scan base-package=""></context:component-scan>标签
其中base-package=""的值为要扫描的包eg:base-package="com.tyd.spring"
3.3创建Java项目:
笔者代码:
创建时只需创建一个普通的class然后在class类中添加@Controller并导入相关的包
建立连接:创建一个方法进行相关操作在方法上添加 @RequestMapping()括号中时连接名,下面是笔者的一个简单代码示范:
package com.tyd.spring; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class MySpring { @RequestMapping(value="/sayOK") public String sayOk(){ return "/WEB-INF/view/success.jsp"; } }
jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SpringMVC</title>
</head>
<body>
<h1><a href="${pageContext.request.contextPath}/sayOK">welcome</a></h1>
</body>
</html>