SpringMVC学习(一)-HelloWorld
来源:互联网 发布:中国数据网 编辑:程序博客网 时间:2024/05/16 10:42
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;@Controllerpublic 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>
阅读全文
0 0
- SpringMVC学习(一)-HelloWorld
- SpringMVC学习之helloworld(一)
- SpringMVC学习(一)之HelloWorld
- 学习SpringMVC(一)之helloworld
- springmvc学习笔记(一)第一个helloworld
- SpringMVC学习(一)环境搭建以及HelloWorld
- SpringMVC学习(1):HelloWorld
- SpringMVC入门一:helloWorld
- SpringMVC(一)HelloWorld
- SpringMVC学习(1.helloworld)
- SpringMVC学习第一章:HelloWorld
- SpringMVC学习笔记(一)使用IntelliJ IDEA开发Spring MVC HelloWorld
- SpringMVC学习笔记(一)-概述、HelloWorld、@RequestMapping、@RequestParam
- JAXWS学习(一)- HelloWorld
- swift学习(一)HelloWorld
- MyBatis学习(一)-HelloWorld
- 一、SpringMVC入门,实现HelloWorld
- springMVC学习(一)
- java操作文件,字节流与字符流介绍
- cookie和session的详解
- MOS管驱动电路,看这里就啥都懂了!
- <8/23>集训日记
- 垃圾收集算法和垃圾收集器
- SpringMVC学习(一)-HelloWorld
- 常用电平标准及解析
- cookie和session的超详解(配图)
- 技术人员如何提升自己的价值
- 模拟地与数字地详解
- 常用的排序算法
- github -ubuntu 首次git clone 代码错误Please make sure you have the correct access rights and the repositor
- 面向对象-选课系统
- 成年人的思想还能进步么?