能在页面打印出HelloWord首先得搭建SpringMvc环境。
第一步:导入所需的Spring架包!(这些架包均来自Spring官网下载的Spring-framework)
第二部:配置web.xml文件
在许多Mvc框架中,都包含一个用于调度控制控制的Servlet。而在SpringMvc中也提供了名为org.springframework.web.servlet.DispatcherServlet的Servlet充当控制器,Spring Mvc所有的请求都要围绕这个DispatcherServlet来分派请求,因为DispatcherServlet也是一个Servlet,所以需要在web.xml中配置一下。
进入web.xml文件中 Alt + /
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>location</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</url-pattern>
</servlet-mapping>
在Web应用启动后该文件会立即被加载,DispatcherServlet加载时会需要一个SpringMVC的配置文件,默认情况下他回去WEB-INF文件夹下查找对应的[servlet-name]-servlet.xml文件。
所以这里我们需要注释掉
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>location</param-value>
</init-param>
也可以将配置文件放在任何地方用<param-value>和<param-name>属性来配置路径和文件名。
这里我使用的是默认情况下。
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置DispatcherServlet的一个初始化参数:配置SpringMvc 配置文件的位置和名称-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
修改<url-pattern>/</url-pattern>目的是将所有的url映射到Servlet
第三步:配置Spring Mvc的配置文件。
将他与web.xml放在同目录下,名字为
创建HelloWord.java
package com.sgf.springmvc.handlers;
import java.util.Date;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
@RequestMapping("/helloworld")
public String hello() {
System.out.println("HelloWorld");
return "success";
}
}
返回值会通过视图解析器解析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器, 会做如下的解析:
通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图, 然会做转发操作
/WEB-INF/views/success.jsp@RequestMapping是来映射请求URL
接下来配置.springDispatcherServlet-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: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">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.sgf.springmvc"></context:component-scan>
<!--配置视图解析器:如何把handler方法的返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
在创建两个jsp页面
一个在根目录下index.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>
<a href="helloworld">Hello World</a>
</body>
</html>
一个在WEB-INF/views/success.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>
<h5>success</h5>
</body>
</html>
点击