您的位置:首页 > 编程语言 > Java开发

SpringMVC学习(一)-HelloWorld

2017-08-23 23:57 316 查看
SpringMVC实现的步骤

1、加入相关jar包,在maven项目里就是在pom.xml文件中引入相关实际依赖

2、在web.xml文件中配置DispatcherServlet

3、加入springmvc配置文件

4、编写处理请求的请求处理器,并标识为处理器

5、编写视图

1.web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 配置DispatcherServlet -->
<!-- 配置DispatcherServlet的作用是:如果在某个方法上配置了@RequestMapping("/helloworld"),
当浏览器访问helloworld时,DispatcherServlet会将这个请求发送给@RequestMapping("/helloworld")
所在的方法上,执行这个方法 -->
<servlet>
<servlet-name>SpringDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置DispatcherServlet的一个初始化参数:配置springMVC配置文件的位置和名称 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--SpringDispatcherServlet在当前web应用被加载的时候被创建,而不是等第一次请求的时候被创建  -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>  <!-- 可以应答所有请求 -->
</servlet-mapping>

</web-app>


2.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"
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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.springMVC"></context:component-scan>

<!-- 配置视图解析器:如何把handler方法返回值解析为实际视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>


3.java类

package com.springMVC.Helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Helloworld {
/*
* 1.使用@RequestMapping注解来映射请求的URL
* 2.返回值会通过视图解析器为实际的物理视图,对于InternalResourceViewResolver
*通过prefix+returnVal+后缀 这样的方式得到实际的物理视图,然后做转发操作
* /WEB-INF/views/success.jsp
*/

@RequestMapping("/helloworld")
public String  hello() {
System.out.println("hello world");
return "success";
}

}


4.简单jsp页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>
<a href="helloworld">Hello World</a>

</body>
</html>


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>
<h2>success page</h2>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mvc web.xml