开发环境:
操作系统Win10。
1.下载Java 15,提取码:soft
2.下载软件 Eclipse 2020-12,提取码:soft
3.开发环境配置(第1篇) Java Web开发(Eclipse+SpringBoot+Jsp),提取码:soft
1.工程目录结构
2. 代码内容
2.1 登录画面Form类,ACMForm.java
package satuday.naruto.webapp.controller.acm;
public class ACMForm {
private String username;
private String password;
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;
}
}
2.2 登录画面Controller类,ACMController.java
package satuday.naruto.webapp.controller.acm;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import satuday.naruto.webapp.controller.ind.INDForm;
/**
* 登录画面的Controller
*
*/
@Controller
public class ACMController {
/** 画面初始化事件 */
private static final String EVT_INIT= "acm001";
/** 画面登录事件 */
private static final String EVT_LOGIN = "acm002";
/**
* 画面的初始化事件
* @param m
* @return
*/
@RequestMapping(EVT_INIT)
public String init(Model m) {
// 视图重定向/acm/acm.jsp
return "acm/acm01";
}
/**
* 画面的登录事件
* @param m
* @param scmform
* @return
*/
@RequestMapping(value = EVT_LOGIN, method = RequestMethod.POST)
public String login(Model m, @ModelAttribute("acmform") ACMForm scmform) {
if(!"admin".equals(scmform.getUsername()) || !"123456".equals(scmform.getPassword())) {
// 登录失败
return "acm/acm02";
}
// 登录成功跳转
INDForm indform = new INDForm();
indform.setUsername(scmform.getUsername());
indform.setPassword(scmform.getPassword());
m.addAttribute("indform", indform);
// 视图重定向index.jsp
return "index";
}
}
2.3 主画面Form类,INDForm.java
package satuday.naruto.webapp.controller.ind;
public class INDForm {
private String username;
private String password;
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;
}
}
2.4 主画面Controller类,INDController.java
package satuday.naruto.webapp.controller.ind;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import satuday.naruto.webapp.controller.acm.ACMForm;
/**
* 主界面的Controller
*
*/
@Controller
public class INDController {
/** 画面初始化事件 */
private static final String EVT_INIT= "/";
/**
* 画面的初始化事件
* @param m
* @return
*/
@RequestMapping(EVT_INIT)
public String init(Model m) {
INDForm indform = new INDForm();
indform.setUsername("未录入用户名");
indform.setPassword("未录入密码");
m.addAttribute("indform", indform);
// 视图重定向index.jsp
return "index";
}
}
2.5 Web启动类,WebAppMainApplication.java
package satuday.naruto.webapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 启动类
*
*/
@SpringBootApplication
public class WebAppMainApplication {
/**
* Web启动主方法
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(WebAppMainApplication.class, args);
}
}
2.6 配置文件,application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
2.7 前台登录画面,acm01.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录界面</title>
</head>
<body>
<form action="acm002" name="acmform" method="post">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Username">
<br>
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
<button type="submit" class="btn btn-default">Submit</button>
</form>
</body>
</html>
2.8 前台登录失败画面,acm02.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
账号登录失败!<br>
<a href="acm001">重新登录</a>
</body>
</html>
2.9 前台主画面,index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
你的名字是 ${indform.username}<br>
你的密码是 ${indform.password}<br>
<a href="acm001">登录</a>
</body>
</html>
2.10 依赖,pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<artifactId>Naruto</artifactId>
<groupId>Satuday</groupId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<!-- 依赖管理,引入以后在声明其它dependency的时候就不需要version -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath />
</parent>
<properties>
<!-- 声明项目配置依赖编码格式为 utf-8 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- java jdk 版本声明 可变更 根据自己配置去匹配 -->
<java.version>1.15</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<fastjson.version>1.2.24</fastjson.version>
</properties>
<dependencies>
<!-- 对web开发的支持,包含了spring webmvc和tomcat等web开发的特性 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot核心,包括自动配置支持,日志和YAML -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。 -->
<!-- 如果使用maven的spring-boot:run的话就不需要此配置 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. 最终画面效果
3.1 启动工程(运行WebAppMainApplication.java)
3.2 网页浏览器地址栏输入:http://localhost:8080/。
3.3 点击"登录"链接,输入用户名“曹操”,密码“567”,并点击Submit按钮,结果登录失败。
3.4 点击“重新登录”链接,并输入用户名“admin”,密码“123456”,点击Submit按钮,结果登录成功,并获得后台传递过来的用户名和密码。