Spring MVC---3.0 简单入门以及相关理解
下面是自己关于Spring MVC简单的入门例子----以及对于Spring MVC使用的相关理解
----------jar 包如下
jstl-1.2.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar(也即过去的spring mvc jar package)
org.springframework.web-3.0.5.RELEASE.jar
commons-logging.jar
---------------------------------------示例如下----------------------------
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- <init-param>-->
<!-- <param-name>contextConfigLocation</param-name>-->
<!-- <param-value>/WEB-INF/applicationContext.xml</param-value>-->
<!-- </init-param>-->
<!--
applicationContext.xml文件代表示应用程序服务的配置和 bean 配置。如果想装入多个配置文件,可以在
<param-value>标记中用逗号作分隔符。
springmvc配置文件与spring配置的servlet名称有关[如本配置中是dd]
通常springmvc配置文件名称结构为:[servlet-name]-servlet.xml,
如果你没有指定init-param里面contextCofigLocation的值中对应的XML文件的话
(也就是applicationContext全局配置文件没有配置在web.xml中的话),那么像本
段代码对应在springmvc中的配置文件就应该是/WEB-INF/dipatcher-servlet.xml这样的文件,
否则如果配置了applicaitonContext.xml这样的spring全局配置文件,如本配置那么就必须为/WEB-INF/dd-servlet.xml
-->
<!-- 配置拦截*.do一类的请求 -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 默认跳转到根目录下的页面,在这里为index.jsp -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
-----<servlet-name>-servlet.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:p="http://www.springframework.org/schema/p" 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 默认扫描的包路径 -->
<context:component-scan base-package="cn.org.spartacus.spring" />
<!-- 添加注解驱动 -->
<mvc:annotation-driven />
<!-- 定义跳转的文件的前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<!-- 使用的视图显示类 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<!-- 视图的前缀/WEB-INF/jsp/,对应于目录/MySpringMVC/WebRoot/WEB-INF/jsp/return.jsp -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 视图显示后缀名,也即*/return.jsp -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
----------------工程根目录下的index.jsp----------------------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘index.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<!-- 用来实现跳转,验证spring成功与否 -->
<body>
<!--使用中间链接的形式 -->
<!-- <a href="hello.do">Say Hello</a>
-->
<!-- 直接使用重定向 hello.do中的hello对应于HelloWorldController中的注解请求@RequestMapping("/clark") -->
<c:redirect url="/clark.do"></c:redirect>
</body>
</html>
------------------处理请求的spring controller 控制处理逻辑类--------------------
package cn.org.spartacus.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
//声明Controller为注解形式
@Controller
public class HelloWorldController {
/**
* @RequestMapping("/clark") 中的值clark与请求返回过来的*.do的*值对应---此步骤关键
* 关于方法名doAction()可以随意命名,与结果无关
* @return
*/
@RequestMapping("/clark")
public ModelAndView doAction() {
String message = "hello World,Spring 3.0 MVC!";
System.out.println(message);
/*
* 返回ModelAndView对象
* return---->对应着view显示的视图名称return.jsp
* 设置"message"----message
*/
return new ModelAndView("return","message",message);
}
}
---------------------返回的View 显示 return.jsp--------------------------
package cn.org.spartacus.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
//声明Controller为注解形式
@Controller
public class HelloWorldController {
/**
* @RequestMapping("/clark") 中的值clark与请求返回过来的*.do的*值对应---此步骤关键
* 关于方法名doAction()可以随意命名,与结果无关
* @return
*/
@RequestMapping("/clark")
public ModelAndView doAction() {
String message = "hello World,Spring 3.0 MVC!";
System.out.println(message);
/*
* 返回ModelAndView对象
* return---->对应着view显示的视图名称return.jsp
* 设置"message"----message
*/
return new ModelAndView("return","message",message);
}
}
=================需建立相应的jsp对应的目录===================
部署好之后地址栏输入:http://localhost:8080/部署上的工程名/,就会显示相应的输出结果:
hello World,Spring 3.0 MVC!
----------jar 包如下
jstl-1.2.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar(也即过去的spring mvc jar package)
org.springframework.web-3.0.5.RELEASE.jar
commons-logging.jar
---------------------------------------示例如下----------------------------
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- <init-param>-->
<!-- <param-name>contextConfigLocation</param-name>-->
<!-- <param-value>/WEB-INF/applicationContext.xml</param-value>-->
<!-- </init-param>-->
<!--
applicationContext.xml文件代表示应用程序服务的配置和 bean 配置。如果想装入多个配置文件,可以在
<param-value>标记中用逗号作分隔符。
springmvc配置文件与spring配置的servlet名称有关[如本配置中是dd]
通常springmvc配置文件名称结构为:[servlet-name]-servlet.xml,
如果你没有指定init-param里面contextCofigLocation的值中对应的XML文件的话
(也就是applicationContext全局配置文件没有配置在web.xml中的话),那么像本
段代码对应在springmvc中的配置文件就应该是/WEB-INF/dipatcher-servlet.xml这样的文件,
否则如果配置了applicaitonContext.xml这样的spring全局配置文件,如本配置那么就必须为/WEB-INF/dd-servlet.xml
-->
<!-- 配置拦截*.do一类的请求 -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 默认跳转到根目录下的页面,在这里为index.jsp -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
-----<servlet-name>-servlet.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:p="http://www.springframework.org/schema/p" 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 默认扫描的包路径 -->
<context:component-scan base-package="cn.org.spartacus.spring" />
<!-- 添加注解驱动 -->
<mvc:annotation-driven />
<!-- 定义跳转的文件的前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<!-- 使用的视图显示类 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<!-- 视图的前缀/WEB-INF/jsp/,对应于目录/MySpringMVC/WebRoot/WEB-INF/jsp/return.jsp -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 视图显示后缀名,也即*/return.jsp -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
----------------工程根目录下的index.jsp----------------------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘index.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<!-- 用来实现跳转,验证spring成功与否 -->
<body>
<!--使用中间链接的形式 -->
<!-- <a href="hello.do">Say Hello</a>
-->
<!-- 直接使用重定向 hello.do中的hello对应于HelloWorldController中的注解请求@RequestMapping("/clark") -->
<c:redirect url="/clark.do"></c:redirect>
</body>
</html>
------------------处理请求的spring controller 控制处理逻辑类--------------------
package cn.org.spartacus.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
//声明Controller为注解形式
@Controller
public class HelloWorldController {
/**
* @RequestMapping("/clark") 中的值clark与请求返回过来的*.do的*值对应---此步骤关键
* 关于方法名doAction()可以随意命名,与结果无关
* @return
*/
@RequestMapping("/clark")
public ModelAndView doAction() {
String message = "hello World,Spring 3.0 MVC!";
System.out.println(message);
/*
* 返回ModelAndView对象
* return---->对应着view显示的视图名称return.jsp
* 设置"message"----message
*/
return new ModelAndView("return","message",message);
}
}
---------------------返回的View 显示 return.jsp--------------------------
package cn.org.spartacus.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
//声明Controller为注解形式
@Controller
public class HelloWorldController {
/**
* @RequestMapping("/clark") 中的值clark与请求返回过来的*.do的*值对应---此步骤关键
* 关于方法名doAction()可以随意命名,与结果无关
* @return
*/
@RequestMapping("/clark")
public ModelAndView doAction() {
String message = "hello World,Spring 3.0 MVC!";
System.out.println(message);
/*
* 返回ModelAndView对象
* return---->对应着view显示的视图名称return.jsp
* 设置"message"----message
*/
return new ModelAndView("return","message",message);
}
}
=================需建立相应的jsp对应的目录===================
部署好之后地址栏输入:http://localhost:8080/部署上的工程名/,就会显示相应的输出结果:
hello World,Spring 3.0 MVC!