SpringMVC框架学习(1)——HelloWorld
程序员文章站
2022-06-02 21:21:12
...
1.创建一个名为spring26-001-hello的Maven项目,结构如下
2.引入所需jar包的依赖坐标pom.xml,代码如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yc</groupId>
<artifactId>spring26-001-hello</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring26-001-hello Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- springmvc的依赖坐标 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<finalName>spring26-001-hello</finalName>
</build>
</project>
3.创建SpringMVC的配置文件spring-mvc.xml和web.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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描指定包中,要对象交给springmvc容器管理的类 -->
<context:component-scan base-package="com.yc.springmvc"/>
<!-- 配置springmvc视图处理器的bean -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
<?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">
<!-- 请求方式转换:把post请求处理转换为put或delete请求方式 -->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置请求由springmvc框架处理 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern><!-- 不拦截处理xxx.jsp -->
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.创建处理类HelloHandler.java,代码如下
package com.yc.springmvc.web.handler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/springmvc") //请求处理的前缀,可以做过滤处理 /user/*
public class HelloHandler {
@RequestMapping("/hello") //具体的请求资源处理
public String hello(){
System.out.println("请求HelloHandler.hello()处理进来了...");
//return "redirect:success.jsp";
return "forward:/success.jsp";
}
/**
* RequestMapper的参数
* params
* headers
* method
* consumes
* produces
* 用来做限制请求条件,主要有"="和"!="
* @return
*/
@RequestMapping(value="/hello02",params={"name!=yc"}) //具体的请求资源处理
public String hello02(){
System.out.println("请求HelloHandler.hello02()处理进来了...");
//return "redirect:success.jsp";
return "forward:/success.jsp";
}
/**
* method 可以用来做rest风格处理
* get ==>取到资源
* post ==>插入资源
* put ==>更新资源
* delete ==>删除资源
* @return
*/
@RequestMapping(value="/hello03",method=RequestMethod.GET) //具体的请求资源处理
public String hello03(){
System.out.println("请求HelloHandler.hello03()处理进来了...");
//return "redirect:success.jsp";
return "forward:/success.jsp";
}
@RequestMapping(value="/hello04",method=RequestMethod.POST) //具体的请求资源处理
public String hello04(){
System.out.println("请求HelloHandler.hello04()处理进来了...");
//return "redirect:success.jsp";
return "forward:/success.jsp";
}
@RequestMapping(value="/hello05",method=RequestMethod.PUT) //具体的请求资源处理
public String hello05(){
System.out.println("请求HelloHandler.hello05()处理进来了...");
//return "redirect:success.jsp";
return "forward:/success.jsp";
}
@RequestMapping(value="/hello06",method=RequestMethod.DELETE) //具体的请求资源处理
public String hello06(){
System.out.println("请求HelloHandler.hello06()处理进来了...");
//return "redirect:success.jsp";
return "forward:/success.jsp";
}
}
5.前台界面如下
<html>
<body>
<h2>Hello World!</h2>
<form action="springmvc/hello04" method="post">
<button>post请求</button>
</form>
<form action="springmvc/hello05" method="post">
<input type="hidden" value="PUT" name="_method">
<button>put请求</button>
</form>
<form action="springmvc/hello06" method="post">
<input type="hidden" value="DELETE" name="_method">
<button>delete请求</button>
</form>
</body>
</html>
跳转界面如下
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title</title>
</head>
<h1>你好,我是springmvc框架,欢迎你使用我,我是不是很帅!!!</h1>
<body>
</body>
</html>
6.运行结果
点击按钮之前出现
上一篇: springMVC 框架搭建学习
下一篇: [二分][假·数论]分数