个人对SpringMVC的理解,SpringMVC是用来取代Servlet的,Servlet中存在的问题就是页面之间的跳转比较麻烦,尤其是处理多个URL地址进行分配,SpringMVC框架可以有效的处理地址的跳转,以及对参数的获取比较方便,再者SpringMVC比较灵活,里面封装好了多个API方法。
下面是SpringMVC的一个基本案例
其步骤如下
1.配置web.xml(放置在lib包下)
2.配置springMVC.xml(放置在src下面)
3.写普通Java类
介绍一下基本的流程,运行hello.jsp文件,点击超链接,会寻址到HelloController ,HelloController会进行逻辑处理,进行跳转到result.jsp
配置web.xml
一个web项目基本是少不了web的配置,一般情况下web.xml属于是默认的配置状况,加入框架更是要在web.xml中进行配置说明,这样项目在启动的时候就会加载配置。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringMVC</display-name>
<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:springMVC.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
配置springMVC.xml(放置在src下面)
SpringMVC属于Spring的一部分,但是又不是完全相同,同样的这个框架也是基于配置的框架。启用注解,在普通的Java类中写上注解,SpringMVC进行包扫描的方式进行扫描,凡是写了注解的这个类就会由普通Java类成为SpringMVC所管理的bean。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:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--
1.请求线 配置普通Java类为Spring的bean
2.视图解析器:响应的名字,真正的响应
-->
<!--启用注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!--用包扫描的方式,有注解的类就会变成Sping上下文空间里的bean -->
<context:component-scan base-package="controller"></context:component-scan>
<!--视图解析器本身就是一个bean -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
引入基本的jar包
写一个普通java类
HelloController.java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello.action")
//下面这个配置项是用于限制Http请求方法的
//@RequestMapping(value="/hello.action", method= {RequestMethod.POST})
public String hello() {
System.out.println("hello world");
return "result";
}
}
一个简单的页面
hello.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>Insert title here</title>
</head>
<body>
<!--在HelloController这个类中配置了一个RequestMapping叫做'/hello.action' -->
<a href="hello.action">点击跳到结果页面</a>
</body>
</html>
一个简单的结果页面
result.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>result</title>
</head>
<body>
<h1>结果页面</h1>
</body>
</html>