文章目录
- 一、springMVC框架介绍
- 二、springMVC框架的搭建
- 第一步:导入springMVC的jar包
- 第二步:书写主配置文件:springmvc.xml、web.xml
- 第三步:启动tomcat测试
- @Controller:
- @RequestMapping
- @Scope注解:
- @AutoWired注解:
- @RequestParam注解:
一、springMVC框架介绍
- 所需储备知识点:
Servlet、jsp、spring框架 - springMVC主要功能:
在获取多个参数、文件上传、servelt功能单一方面都能有很好的解决办法
SpringMVC工作原理图:
二、springMVC框架的搭建
第一步:导入springMVC的jar包
第二步:书写主配置文件:springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!-- 配置包扫描 -->
<context:component-scan base-package="cn.java.controller"></context:component-scan>
<!-- 加入springMVC特有的注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
</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">
<display-name>springMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置springMVC的核心控制器类:DispatcherServlet -->
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
第三步:启动tomcat测试
package cn.java.controller.front;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.java.entity.User;
@Controller//如果一个普通类被@Controller注解,则普通类变成一个servlet
public class FrontController {
@RequestMapping(value={"/login.htm", "a.htm", "b.htm"})//为当前方法配置一个对外访问的虚拟路径
public void Login(User user){
System.out.println("登录成功");
System.out.println(user);
}
@RequestMapping(value="/regiter.htm", method=RequestMethod.POST)//value和/可以省略
public String regiter(User user){
System.out.println(user);
System.out.println("注册成功");
return "/success.jsp";
}
}
package cn.java.entity;
public class User {
private String username;
private String password;
private Integer age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", age=" + age + "]";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-3.3.1.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
<form action="<%=basePath%>/regiter.htm" method="post">
<p>用户名:<input type="text" name="username"></p>
<p>密 码:<input type="password" name="password"></p>
<p>年 龄:<input type="text" name="age"></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-3.3.1.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
注册成功
</body>
</html>
结果是:
User [username=matthew, password=1234, age=23]
注册成功
@Controller:
如果一个普通类被@Controller注解,则普通类变成一个servlet
用户用表单传递的参数可以直接被方法接受,前提名称一致。
@RequestMapping
为当前方法配置一个对外访问的虚拟路径。
可以设置多个,用大括号括起来中间用逗号隔开。
value和=可以省略
让方法以post方式接受:method=RequestMethod.POST 如果不写这个参数post和get都可以接受。
也可以将尾缀省略,放置web配置文件更改
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
加入把url中的值改为.shtml那么所有的类都要更改所以省略就省却这个麻烦,框架会自动补全。
@RequestMapping(value = "/test1")
public void test1(User user){
System.out.println("test1.......");
System.out.println(user);
}
@Scope注解:
@AutoWired注解:
实现依赖注入,参见spring的注解。
@RequestParam注解:
<form action="<%=basePath%>/test5.htm" method="post">
<p>用户名:<input type="text" name="user"></p>
<p>密 码:<input type="password" name="pwd"></p>
<p>年 龄:<input type="text" name="nianling"></p>
<p><input type="submit" value="提交"></p>
</form>
@RequestMapping(value = "/test2")
//将name中的参数对应的值传给注解修饰的参数例如将user传给username
public void test2(@RequestParam(name = "user", defaultValue = "张三", required = false) String username,
@RequestParam(name = "pwd", required = true) String password, @RequestParam(name = "nianling") Integer age){
System.out.println("username" + "=" + username);
System.out.println("password" + "=" + password);
System.out.println("age" + "=" + age);
}
当前端和后端的代码分别由两个人完成时,当前端代码更改,后端不得不更改时,只需要将@RequestParam中的name属性更改就行了。
required属性:为true说明必须传值,否则会报错,而false一般与defaultValue属性搭配使用。
@RequestMapping(value="submitYijian")
public void submitYIjian(@RequestParam(required = true) String content,
@RequestParam(required = false) String phoneNum, @RequestParam(required = false) String email){
}