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
2.springmvc.xml
3.java类
4.简单jsp页面
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>
相关文章推荐
- SpringMVC学习系列(2) 之 经典的HelloWorld实现
- SpringMVC学习-HelloWorld
- SpringMVC学习系列(2) 之 经典的HelloWorld实现
- 学习SpringMVC——从HelloWorld开始
- SpringMVC学习系列(2) 之 经典的HelloWorld实现
- SpringMVC学习系列(2) 之 经典的HelloWorld实现
- SpringMVC学习笔记(3) 框架配置+HelloWorld(非注解版)
- 学习SpringMVC——从HelloWorld开始
- 学习SpringMVC——从HelloWorld开始
- 学习SpringMVC——从HelloWorld开始
- springmvc学习笔记(1)——搭建springmvc项目之HelloWorld
- 学习SpringMVC——从HelloWorld开始
- SpringMVC学习系列(2) 之 经典的HelloWorld实现
- 学习SpringMVC——从HelloWorld开始
- SpringMVC学习系列(2)之 经典的HelloWorld实现
- SpringMVC框架学习(1)——HelloWorld
- SpringMVC学习笔记(一)-概述、HelloWorld、@RequestMapping、@RequestParam
- 学习SpringMVC——从HelloWorld开始,简单快速上手springmvc
- SpringMVC学习系列(2) 之 经典的HelloWorld实现
- SpringMVC学习系列(3) 之 URL请求到Action的映射规则 在系列(2)中我们展示了一个简单的get请求,并返回了一个简单的helloworld页面。本篇我们来学习如何来配置一个acti