SpringMVC
5. SpringMVC案例展示
5.1 创建如下项目结构
5.2 在src下的com.springmvc包下创建Student.java
package com.springmvc;
/**
* @author 北大青鸟南京中博 Holly老师
* 学IT 就找Holly老师
* QQ/微信:964918306
*/
public class Student {
private Integer sid;
private String sname;
private String password;
public Student() {
}
public Student(String sname, String password) {
this.sname = sname;
this.password = password;
public Student(Integer sid, String sname, String password) {
this.sid = sid;
public Integer getSid() {
return sid;
public void setSid(Integer sid) {
public String getSname() {
return sname;
public void setSname(String sname) {
public String getPassword() {
return password;
public void setPassword(String password) {
@Override
public String toString() {
return "Student [password=" + password + ", sid=" + sid + ", sname="
+ sname + "]";
5.3 在src下的com.springmvc包下创建SpringMVCController.java
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
* @Controller 标注控制类,负责注册一个bean到Spring上下文
* @RequestMapping 标注控制器指定可以处理那些url请求
*
@Controller
public class SpringMVCController {
//1.自动匹配请求
@RequestMapping("/hello.do")
public String hello(HttpServletRequest request,HttpServletResponse response){
Student stu=new Student("holly", "123");
request.setAttribute("stu", stu);
//跳转方式,走底层的默认转发
return "/index.jsp";
//2.登录请求映射
@RequestMapping("/login.do")
public void login(HttpServletRequest request,HttpServletResponse response,String sname,String password) throws IOException{
Student stu=new Student(sname, password);
System.out.println("登录的用户:"+stu);
request.getSession().setAttribute("stu", stu);
response.sendRedirect("index.jsp");
//3.注册请求映射
@RequestMapping("/register.do")
public String register(HttpServletRequest request,Student stu) throws IOException{
System.out.println("注册的用户:"+stu);
return "index.jsp";
5.4 在WebRoot下的WEB-INF下创建springMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 1.配置自动扫描的包:全局扫描包 -->
<context:component-scan base-package="com.springmvc"/>
<!-- 2.mvc支持注解(自动匹配):mvc注解驱动 -->
<mvc:annotation-driven/>
<!-- 3.如果当请求为"/"时,则转发到hello请求,也就是默认首启项设置,
web.xml的欢迎列表不写,url-pattern需要是/ -->
<mvc:view-controller path="/" view-name="forward:/hello.do"/>
</beans>
5.5 在WebRoot下的WEB-INF下创建web.xml
<web-app version="2.5"
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_2_5.xsd">
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
</web-app>
5.6 在WebRoot下创建login.jsp
<%@ page language="java" import="java.util.*" 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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<fieldset>
<legend>登录</legend>
<form action="login.do" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="sname"/></td>
</tr>
<td>密码:</td>
<td><input type="password" name="password"/></td>
<td><input type="submit" value="提交"/></td>
<td><input type="reset" value="重置"/></td>
</table>
</form>
</fieldset>
</center>
</body>
</html>
5.7 在WebRoot下创建register.jsp
<legend>注册</legend>
<form action="register.do" method="post">
5.8 在WebRoot下创建index.jsp
<title>My JSP 'index.jsp' starting page</title>
欢迎${stu.sname }来首页
5.9 项目效果
开发人员转岗好消息!!
以上代码纯属原创 ,为了能够共同进步互相学习,如有问题或更好建议可以联系holly老师:
每周五晚: 斗鱼直播讲堂(房间号672217)
(4月7日主题:通过生活聊java)
博客园: 红酒人生(有大量技术帖子)
想学习java,安卓,大数据,数据库,web开发,前端开发 可以来“北大青鸟南京中博软件学院”找 Holly老师 ,欢迎来咨询!
长按下方二维码关注Holly老师公众号,一起学java吧!