Maven项目 - SpringMvcDemo
单击 【new project】
设置为下图所示后,单击【create】按钮
在pom.xml文件里添加支持Spring MVC的相关依赖,添加内容如下:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>net.army.springgroupId>
<artifactId>SpringMvcDemoartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<spring.version>5.3.25spring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<scope>testscope>
dependency>
dependencies>
project>
选择【modules】-【+】-【web】
单击【Create Artifact】按钮
将名称改为 SpringMvcDemo
,单击【OK】按钮
在WEB-INF里创建views子目录(用于存放JSP页面)
右击【WEB-INF】,选择【new】,单击【Directory】
此时输入views,按回车键
创建成功
在views目录里创建登录页面 - login.jsp
右击【views】,选择【new】,单击【JSP/JSPX】,在输入login,按下回车即可创建成功
添加如下代码:
<%--
Created by IntelliJ IDEA.
User: 梁辰兴
Date: 2023/3/21
Time: 11:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登录title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
head>
<body>
<h3 style="text-align: center">用户登录h3>
<form id="frmLogin" action="login" method="post">
<table class="tb" border="1" cellpadding="10" style="margin: 0px auto">
<tr>
<td align="center">账号td>
<td><input id="username" type="text" name="username"/>td>
tr>
<tr>
<td align="center">密码td>
<td><input id="password" type="password" name="password"/>td>
tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
td>
tr>
table>
form>
body>
html>
在views目录里创建登录成功页面 - success.jsp
添加如下代码:
<%--
Created by IntelliJ IDEA.
User: 梁辰兴
Date: 2023/3/21
Time: 11:14
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>
head>
<body>
<h1 style="text-align: center">${username},登录成功!h1>
body>
html>
在views目录里创建登录失败页面 - failure.jsp
添加如下代码:
<%--
Created by IntelliJ IDEA.
User: 梁辰兴
Date: 2023/3/21
Time: 11:15
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>
head>
<body>
<h1 style="text-align: center">${username},登录失败!h1>
body>
html>
创建net.army.spring.controller包,然后在包里创建LoginController类
右击【net.army.spring】包,选择【new】,单击【package】
输入controller,按下回车键
右击【net.army.spring.controller】包,选择【new】,单击【java class】
输入LoginController,按下回车键
创建成功
添加如下代码:
package net.army.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
/**
* 作者:梁辰兴
* 日期:2023/3/21
* 功能:登录控制器
*/
@Controller // 交给Spring容器来管理
public class LoginController {
@PostMapping("/login") // 可用@RequestMapping("/login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,
HttpSession session) {
// 将登录表单提交的用户名写入会话
session.setAttribute("username", username);
// 判断用户是否登录成功
if (username.equals("army") && password.equals("1234567")) {
// 返回逻辑视图名success,表明跳转到登录成功页面
return "success";
}
// 返回逻辑视图名failure,表明跳转到登录失败页面
return "failure";
}
}
在resources里创建mvc子目录,然后在子目录里创建spring-config.xml
添加如下代码:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="net.huawei.spring" />
beans>
在resources/mvc目录里创建spring-mvc-config.xml
添加如下代码:
<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.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="net.army.spring.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
bean>
beans>
一个Web项目启动时最先要读取web.xml文件
添加如下代码:
<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">
<welcome-file-list>
<welcome-file>/WEB-INF/views/login.jspwelcome-file>
welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:mvc/spring-config.xmlparam-value>
context-param>
<servlet>
<servlet-name>DispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:mvc/spring-mvc-config.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>DispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>Character Encodingfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>Character Encodingfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
单击工具栏上的【current file】-【edit configurations…】
单击【+】-【Tomcat server】-【local】
单击【configure…】
配置如下图,单击【ok】按钮
选择【deployment】,单击【+】
单击【artifact…】
打开【Project Structure】窗口,选择Modules对应的Dependencies
单击【library…】
选择【Tomcat 8.5.86】,单击【add selected】按钮
单击【ok】按钮
单击运行按钮,出现下图错误,可参考《Error:java: 不再支持源选项 5》。
启动失败,因为输出目录没有项目运行所需的jar包
配置Artifacts,添加项目正常运行所需的jar包
选择所有的文件,然后右击,单击【Put into / WEB-INF/lib】
目录添加成功,单击【ok】按钮
再次启动服务,查看结果
下面进行录屏展示登录成功与失败的情况