一,创建项目
其中web文件的创建流程
右键单击项目名,找到Add Framework Support...点开勾选下图选项然后ok即可
二,在lie文件夹中导入jar包并添加(add)
三,在src目录下,创建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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
</beans>
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.smrsar.controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:annotation-driven/>
</beans>
四,创建User和Goods实体类
package com.smrsar.bean;
public class User {
private Integer uid;
private String username;
private String password;
private String phone;
public User() {
}
public User(Integer uid, String username, String password, String phone) {
this.uid = uid;
this.username = username;
this.password = password;
this.phone = phone;
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", username='" + username + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
'}';
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
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 String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
package com.smrsar.bean;
public class Goods {
private Integer gid;
private String gname;
private String price;
@Override
public String toString() {
return "Goods{" +
"gid=" + gid +
", gname='" + gname + '\'' +
", price='" + price + '\'' +
'}';
}
public Integer getGid() {
return gid;
}
public void setGid(Integer gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
五,编写web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</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>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
六,编写一个首页,可以去跳转到登录页
<%--
Created by IntelliJ IDEA.
User: ThinkPad
Date: 2023/4/11
Time: 18:44
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<h2><a href="login.jsp">去登陆</a></h2>
</body>
</html>
七,登录页面
<%--
Created by IntelliJ IDEA.
User: ThinkPad
Date: 2023/4/11
Time: 18:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="login" method="post">
账号:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录"><br>
</form>
</body>
</html>
八,主页面
<%--
Created by IntelliJ IDEA.
User: ThinkPad
Date: 2023/4/11
Time: 19:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1" >
<c:forEach items="${goodsList}" var="goods">
<tr>
<td>${goods.gid}</td>
<td>${goods.gname}</td>
<td>${goods.price}</td>
</tr>
</c:forEach>
</table>
<h1></h1>
</body>
</html>
九,登录失败进入错误页
<%--
Created by IntelliJ IDEA.
User: ThinkPad
Date: 2023/4/11
Time: 19:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>${error}</h1>
</body>
</html>
十,编写登录控制类
package com.smrsar.controller;
import com.smrsar.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
@Controller
@SessionAttributes("msg")//将msg数据的作用域转换为session域
public class TestController {
@RequestMapping("/login")
public ModelAndView Test(User user){
System.out.println(user);
ModelAndView mv=new ModelAndView();
if (user.getUsername().equals("admin")&&user.getPassword().equals("666")) {
mv.setViewName("redirect:/find");//重定向或者请求转发,不经过视图解析器
}else {
mv.addObject("error","账号或密码出错了");
mv.setViewName("error");//跳转指定的视图名
}
return mv;
}
}
十一,编写主页控制类(模拟数据库数据展示)
package com.smrsar.controller;
import com.smrsar.bean.Goods;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;
//模拟数据库
@Controller
@SessionAttributes("goodsList")
public class GoodsController {
@RequestMapping("/find")
public ModelAndView FindAll() {
ModelAndView mv=new ModelAndView();
List<Goods> goodsList=new ArrayList<>();
Goods g1=new Goods();
g1.setGid(1);
g1.setGname("sd");
g1.setPrice("21");
Goods g2=new Goods();
g2.setGid(2);
g2.setGname("sd");
g2.setPrice("21");
Goods g3=new Goods();
g3.setGid(3);
g3.setGname("sd");
g3.setPrice("21");
Goods g4=new Goods();
g4.setGid(4);
g4.setGname("sd");
g4.setPrice("21");
goodsList.add(g1);
goodsList.add(g2);
goodsList.add(g3);
goodsList.add(g4);
mv.addObject("goodsList",goodsList);
mv.setViewName("redirect:/zhuye.jsp");
return mv;
}
}
结果图示
1,首页
2,登录页
3,错误页
4,登录成功进入主页