这几天在学SSM,暂时总结一下,本着先跑起来再增加功能的原则和helloWrold原则先从helloWrold开始,暂时只涉及了SpringMVC和Spring
先看我的工程目录,因为是helloWrold所以src先暂时只要建com.controller包就行,mybatis暂时没用到
一、搭建环境
框架本身是为了方便我们使用的,但是环境的搭建往往让我们望而却步(一点小错步步错啊~)
- 导入jar包:我把自己用到的jar上传了,地址http://download.csdn.net/download/qq_20367813/9944287
- 配置web.xml:web.xml主要配置了Spring和SpringMVC的路径以及监听事件,配置如下
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- spring的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置Spring MVC 的拦截器,将会拦截所有路径下的请求 --> <!-- 配置DispatchcerServlet --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置Spring mvc下的配置文件的位置和名称 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
- 配置Spring:Spring是项目的粘合剂,配置的重中之重,web.xml中配置了路径为src下面的application.xml,因此在src下建立application.xml文件,配置如下
其实Spring很多功能没用到,只是个helloWrold小例子,暂时先跑起来<?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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:annotation-config /> <context:component-scan base-package="com" /> </beans>
- 配置SpringMVC:SpringMVC作为拦截器主要是拦截前端的连接经过处理再跳到前台,配置了跳转的视图解析器和控制类扫描路径
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- 自动扫描该包,SpringMVC会将包下用了@controller注解的类注册为Spring的controller --> <context:component-scan base-package="com.controller"/> <!-- 使用默认的Servlet来响应静态文件 --> <mvc:default-servlet-handler/> <!-- 设置配置方案 --> <mvc:annotation-driven/> <!-- 视图解析器 ,将方法返回结果拼接成url路径跳转 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix"> <value>/WEB-INF/</value> </property> <!-- 后缀 --> <property name="suffix"> <value>.jsp</value> </property> <property name="order" value="0" /> </bean> </beans>
到这,一个helloWrold的环境搭建就完成了,mybatis的配置没有涉及到
二、web页面编写
一个项目要有个首页~先来个index吧
先不要关注href的跳转路径是hellowrold,点击该链接要有个跳转成功页面success.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <!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>Insert title here</title> </head> <body> <p align="center"><a href="helloworld">Spring-MVC小例子:hello world</a></p> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>hello world , SUCCESS</h1> </body> </html>
三、controller控制类的编写
我们知道SpringMvc拦截了所有的页面跳转请求,比如我们上面index.jsp中href的hellowrold请求,那拦截下来了怎么处理呢?
SpingMVC.xml中配置了
会把com.controller下面的所有类管理起来,然后去类中找hellowrold请求对应的方法,因此我们要在com.controller写一个controller的类,我写了个HelloWrold.java<context:component-scan base-package="com.controller"/>
我们看到有两个注解,@controller和@RequestMapping,说明如注释~~package com.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; //1. 首先要在类的前面添加“Controller”注解,表示是spring的控制器 @Controller public class HelloWorld { //@RequestMapping, 是用于匹配请求的路径:将helloworld请求拦截到这 @RequestMapping("/helloworld") public String hello(){ System.out.println("hello World"); //这个返回的字符串就是与上面springmvc.xml匹配 return "success"; } }
四、HelloWrold
到这,一个简单的基于SSM的小项目就搭起来了,其实更多的是基于SpringMVC,但跑起来后就可以添加各种如Mybatis、Spring的功能了
说一下运行吧
- 通过Tomcat发布项目然后Start服务器,当然直接运行index.jsp也行结果如图
点击链接,控制台会打印hello World同时跳转到success.jsp页面
说明一下流程,当我们运行项目后
这样一个简单的HelloWrold小例子就讲完了,后面我有时间在写一个基于SSM的单表增删改查,等于把Mybaits加入进来了
- 会先加载Web.xml文件,启动了Spring和SpringMVC的监听
- 并且启用了SpringMVC.xml中扫描的controller包下注解了@Controller的类,该类中用@RequestMapping("/****")注解的方法就是/***请求对应的方法
- 当我们点击链接是,拦截hellowrold请求,找到HelloWrold类下的hello(),该方法返回了一个success字符串
- 在SpringMVC中配置了视图解析器,对应@RequestMapping注解下返回的字符串会自动拼接一个地址,然后进行跳转,所以对hello()方法返回的success字符串通过拼接成/WEN-INF/success.jsp,并跳到该界面