我的第一个springmvc
1.SpringMvc的搭建:
①导入jar包
② web.xml文件当中配置DispatcherServlet,通过插件、核心控制器
初始化参数:
contextConfigLocation
DispatcherServlet的配置插件完成
注意事项:
①初始化参数:告知当前springmvc的文件路径
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
②配置当前的ServletMapping,url-pattern修改为/
③配置Spring的文件:必须与web.xml中的DispatcherServlet配置的初始化参数一致
注意事项:
①自动扫描
<context:component-scan base-package="*"></context:component-scan>
②需要配置一个bean,视图解析器,将逻辑视图转化为物理视图
IntervalResourceViewSolver
④配置控制器:
任何的javabean都可以作为控制器,@Controller
方法可以接受请求并且做出相应的响应
Public String 方法名称(){}
视图解析器将返回的Sting解析为物理视图
代码如下:
web.xml:
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<!--
DispacheerServlet通过插件完成
注意事项:1.配置参数:告知springmvc的当前路径
2.配置当前servlet-mapping的url-pattern
-->
<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:springmvc.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>
</web-app>
Springmvc.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"
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.2.xsd">
<!--
1.导入context命名空间 识别标注
2.导入视图解析器:逻辑视图解析(hello)成物理视图(/hello.jsp) InternalResourceViewResolver
-->
<context:component-scan base-package="*"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前置 -->
<property name="prefix" value="/"></property>
<!--后置 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
HelloWorldHandler:
package com.springmvc.first;
/*控制器请求的业务方法的格式:
* public String 方法名称(){}
* String:代表当前方法返回的视图名称
*
*
*
* */
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorldHandler {
@RequestMapping(value="/hello")
public String hello() {
System.out.println("正在进入hello方法");
return "success";
}
}
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>index</title>
</head>
<body>
<a href="hello">HelloWorld</a>
</body>
</html>
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>success</title>
</head>
<body>
sucess page...
</body>
</html>