Spring + Spring MVC + Mybatis 项目搭建和登录注册例子
开发工具:IntelliJ IDEA
数据库:MySql
目录
本篇将记录创建一个 Spring + Spring MVC + Mybatis 项目,并且编写一个注册登录的例子,毕竟Java的东西配置起来真有点麻烦
创建数据库
就随便一点吧,使用MySql,数据库就用 test ,新建一个 users 表,随便设置几个字段
注意
- 要按照命名规范,单词均小写,不同单词之间使用下划线隔开
- Mybatis 貌似不推荐使用外键,需要我们在代码中手动控制实体之间的关系
其实是我在使用 Hibernate 和 Mybatis 的时候,用了很多时间都没解决外键的问题,写配置也觉得挺麻烦的
感觉有时候不如自己封装JDBC,只能感叹 Entity Framework Core 是真的好用(貌似 Mybatis 的外键和 EF Core蛮像的),大概是我还太菜了吧,等我有时间解决外键的问题再写另一篇记录吧 T_T
IDEA 添加数据库
输入用户名和密码,填写连接字符串,然后点击 Test Connection,Schema 可以选择连接的数据库
连接字符串参考:jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
创建项目
使用 Maven 创建 Java Web 项目,选择 maven-archetype-webapp
GroupId:一般是公司域名
ArtifactId:项目名称
毕竟只是个例子,所以就随便写了
配置 maven 路径,这里就不详细展开讲了,Maven 的配置 baidu bing 之类的搜索引擎随便查都有答案
项目路径的最后文件夹与项目名称一致
右键 src
新建几个文件夹
- Controllers:控制器
- DAO:数据库访问
- Filters:过滤器
- Models:实体模型类
- Services:服务层
- Utils:工具类
- Mappers:映射 xml 文件
- Generator:Mybatis Generator 配置文件
- Views:视图
- wwwroot:静态文件,css、js等文件
tomcat
部署
选哪个都可以
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>
<groupId>com</groupId>
<artifactId>ssm_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>ssm_demo Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.Version>4.3.18.RELEASE</spring.Version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<!-- 数据库连接池-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.2</version>
</dependency>
<!-- Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.Version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.Version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.Version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.Version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.Version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.Version}</version>
</dependency>
<!-- Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- mybatis-generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<!-- aop 依赖 -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.20.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.0</version>
</dependency>
<!-- aspectjrt 依赖 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.14</version>
</dependency>
</dependencies>
<build>
<finalName>ssm_demo</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>src/main/resources/Generator/GeneratorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
进入项目结构,Project Structure
因为我偷懒,所以用 tomcat 自带的库,也可以手动在 Maven 添加
主页
Index.jsp,开头的代码主要是获取端口和url的,方便访问静态文件,应该写在过滤器里,这里我就偷懒了
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
String path = basePath + request.getContextPath();
String filePath = path + "/wwwroot";
session.setAttribute("basePath",basePath);
session.setAttribute("path",path);
session.setAttribute("filePath",filePath);
%>
<html>
<head>
<link href="${pageContext.session.getAttribute("filePath")}/css/MyStyleSheet.css" type="text/css" rel="stylesheet">
<title>Home Index</title>
</head>
<body>
<h1>
This is Home Index Page
</h1>
</body>
</html>
WEB-INF/web.xml 添加
<welcome-file-list>
<welcome-file>/Views/Home/Index.jsp</welcome-file>
</welcome-file-list>
还蛮成功
applicationContext.xml 和 springMVC.cml
jdbc.properties ,根据实际情况修改
jdbc.username=root
jdbc.password=123456
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
applicationContext.xml 和 springMVC.xml 都是这样创建,这三个文件都在main目录下
其实有很多配置我也不知道是做什么的,毕竟只是个项目搭建,我水平也没那么高啦,自己捣鼓着玩吧
applicationContext.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:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描main.webapp.DAO包下所有标注@Repository的DAO组件 -->
<context:component-scan base-package="main.webapp.DAO"/>
<!--服务层配置 -->
<!-- 扫描main.webapp.Services包下所有标注@Service的服务组件 -->
<context:component-scan base-package="main.webapp.Services"/>
<!--在扫描时排除Controller-->
<context:component-scan base-package="main.webapp.Controllers">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--Spring整合MyBatis框架-->
<!--配置连接池-->
<context:property-placeholder location="classpath:main/jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
</bean>
<!--配置SqlSessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 告知spring生成哪些接口的实现类 -->
<property name="basePackage" value="main.webapp.DAO"/>
<!-- dao操作数据库时需要sqlSessonfactory,spring会自动找一个id="sqlSessionFactrory"的bean标签 -->
</bean>
<!--配置Spring框架声明式事务管理-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
<tx:method name="Create*" propagation="REQUIRED" read-only="false"/>
<tx:method name="Save*" propagation="REQUIRED" read-only="false"/>
<tx:method name="Reg*" propagation="REQUIRED" read-only="false"/>
<tx:method name="Update*" propagation="REQUIRED" read-only="false"/>
<tx:method name="Delete*" propagation="REQUIRED" read-only="false"/>
<tx:method name="Find*" propagation="REQUIRED" read-only="false"/>
<tx:method name="Get*" propagation="REQUIRED" read-only="false"/>
<tx:method name="Load*" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<!--配置AOP增强-->
<aop:config>
<aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="execution(* main.webapp.DAO.*.*(..)))" order="1"/>
</aop:config>
</beans>
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!--开启SpringMVC注解的支持-->
<mvc:annotation-driven/>
<!--扫描controller层在spring中不在扫描-->
<context:component-scan base-package="main.webapp.Controllers"/>
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/Views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--过滤静态资源-->
<mvc:resources location="wwwroot/css/" mapping="/css/**" />
<mvc:resources location="wwwroot/images/" mapping="/images/**" />
<mvc:resources location="wwwroot/js/" mapping="/js/**" />
</beans>
最后打开项目结构 Project Structure ,如果配置文件不在这里,可以手动添加,或者用 IDEA 的智能提示,进入xml文件时的最上方
配置 Mybatis Generator 自动生成 Model 实体
pom.xml 添加依赖项和插件,其实上面的 pom.xml 已经包含了
<!-- mybatis-generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>src/main/resources/Generator/GeneratorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
添加 Mybatis Generator 插件启动项
添加启动参数 mybatis-generator:generate -e
,这里加了“-e ”选项是为了让该插件输出详细信息,帮助我们定位问题。
resources/Generator 目录下新建 GeneratorConfig.xml 文件
这个配置文件中涉及到路径的节点都可以改
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--mysql 连接数据库jar 这里选择自己本地位置-->
<classPathEntry location="E:\Maven\Repository\mysql\mysql-connector-java\8.0.23\mysql-connector-java-8.0.23.jar" />
<context id="default" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&nullCatalogMeansCurrent=true"
userId="root"
password="123456">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="main.webapp.Models"
targetProject="src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置
如果maven工程只是单独的一个工程,targetProject="src/main/java"
若果maven工程是分模块的工程,targetProject="所属模块的名称",例如:
targetProject="ecps-manager-mapper",下同-->
<sqlMapGenerator targetPackage="main.webapp.Mappers"
targetProject="src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="main.webapp.DAO"
targetProject="src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="users" domainObjectName="UserEntity"></table>
</context>
</generatorConfiguration>
还有一个坑
机翻:
MySql不支持SQL目录和模式。如果在MySql中运行createschema命令,它实际上会创建一个数据库,而JDBC驱动程序会将其报告为一个目录。但是MySql语法不支持标准的catalog..table SQL语法。
因此,最好不要在生成器配置中指定目录或模式。只需在JDBC URL中指定表名和数据库。
如果您使用的是8.x版的Connector/J,您可能会注意到生成器试图为MySql信息模式(sys、information_schema、performance_schema等)中的表生成代码,这可能不是您想要的!要禁用此行为,请将属性“nullCatalogMeansCurrent=true”添加到JDBC URL。
运行 Mybatis Generator 插件启动项
生成的实体类,UserEneity
package main.webapp.Models;
public class UserEntity
{
private Integer id;
private String username;
private String password;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username == null ? null : username.trim();
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password == null ? null : password.trim();
}
}
DAO
MyBatis支持2种类型的映射器:XML映射器和接口映射器,在这里以定义并使用接口映射器为例
自动生成的 Mapper 可能不会加 @Mapper 注解
,要注意,其实不加也可以
@Mapper
public interface UserEntityMapper
{
long countByExample(UserEntityExample example);
int deleteByExample(UserEntityExample example);
int deleteByPrimaryKey(Integer id);
int insert(UserEntity record);
int insertSelective(UserEntity record);
List<UserEntity> selectByExample(UserEntityExample example);
UserEntity selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") UserEntity record, @Param("example") UserEntityExample example);
int updateByExample(@Param("record") UserEntity record, @Param("example") UserEntityExample example);
int updateByPrimaryKeySelective(UserEntity record);
int updateByPrimaryKey(UserEntity record);
@Insert("insert into users(username, password) values(#{username,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR})")
void Save(@Param("username") String username, @Param("password") String password);
@Select("select * from users where username=#{username}")
UserEntity GetUserByUsername(@Param("username") String username);
@Select("select * from users where username=#{username} and password=#{password}")
UserEntity GetUserByUsernameAndPassowrd(@Param("username") String username, @Param("password") String password);
}
Service
@Service
public class UserService
{
@Autowired
private UserEntityMapper _userEntityMapper;
public UserEntity GetUserByUsername(String username)
{
return this._userEntityMapper.GetUserByUsername(username);
}
public UserEntity GetUserByUsernameAndPassword(String username, String password)
{
return this._userEntityMapper.GetUserByUsernameAndPassowrd(username, password);
}
public void Save(UserEntity userEntity)
{
this._userEntityMapper.Save(userEntity.getUsername(), userEntity.getPassword());
}
}
Controller 控制器
控制器这里我返回值使用的是 ModelAndView
主要是懒得写前端代码,其实可以返回任何你想返回的值
@Controller
@RequestMapping("/UserController")
public class UserController
{
@Autowired
private UserService _userService;
@RequestMapping("/Index")
public ModelAndView Index()
{
ModelAndView view = new ModelAndView();
view.setViewName("User/Index");
return view;
}
@RequestMapping(value = "/Login/DoLogin", method = RequestMethod.POST)
public ModelAndView DoLogin(HttpServletRequest request)
{
ModelAndView mav = new ModelAndView();
String username = request.getParameter("username");
String password = request.getParameter("password");
mav.addObject("username", username);
mav.addObject("password", password);
if (request.getParameter("btn").equals("Register"))//可以前确定点击的按钮
{
mav.setViewName("User/Register");
}
else if (request.getParameter("btn").equals("Login"))
{
if (username.equals("") || password.equals(""))
{
mav.addObject("LoginMessage", "用户名或密码不能为空");
mav.setViewName("User/Index");
}
else
{
UserEntity userEntity = this._userService.GetUserByUsernameAndPassword(username, password);
if (userEntity != null)
{
mav.setViewName("User/Success");
}
else
{
mav.addObject("LoginMessage", "用户名或密码错误");
mav.setViewName("User/Index");
}
}
}
return mav;
}
@RequestMapping(value = "/Register/DoRegister", method = RequestMethod.POST)
public ModelAndView DoRegister(HttpServletRequest request)
{
ModelAndView mav = new ModelAndView();
String username = request.getParameter("username");
String password = request.getParameter("password");
String repeatPassword = request.getParameter("repeatPassword");
mav.addObject("username", username);
mav.addObject("password", password);
mav.addObject("repeatPassword", repeatPassword);
if (username.equals("") || password.equals("") || repeatPassword.equals(""))
{
mav.addObject("RegisterMessage", "输入框内容不能为空");
mav.setViewName("User/Register");
}
else if (password.equals(repeatPassword) == false)
{
mav.addObject("RegisterMessage", "密码不一致");
mav.setViewName("User/Register");
}
else
{
UserEntity userEntity = new UserEntity();
if (this._userService.GetUserByUsername(username) == null)
{
userEntity.setUsername(username);
userEntity.setPassword(password);
this._userService.Save(userEntity);
mav.setViewName("User/Index");
}
else
{
mav.addObject("RegisterMessage", "用户名已存在");
mav.setViewName("User/Register");
}
}
return mav;
}
}
jsp 页面
Index.jsp,requestScope
只是用来显示信息
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String context = request.getContextPath();
request.setAttribute("context", context);
%>
<html>
<head>
<link href="${pageContext.session.getAttribute("filePath")}/css/MyStyleSheet.css" type="text/css" rel="stylesheet">
<title>User Index</title>
</head>
<body>
<h1>
This is Login Index Page
</h1>
<form action="${context}/UserController/Login/DoLogin" method="post">
<label>账户:
<input type="text" name="username" value="${requestScope.username}">
</label>
<br>
<label>密码:
<input type="password" name="password" value="${requestScope.password}">
</label>
<br>
<label style="color: red">
${requestScope.LoginMessage}
</label>
<br>
<button type="submit" name="btn" value="Login">登录</button>
<button type="submit" name="btn" value="Register">注册</button>
<a href="${pageContext.session.getAttribute("path")}/Views/User/Register.jsp" style="color: red">注册</a>
</form>
</body>
</html>
Success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<link href="${pageContext.session.getAttribute("filePath")}/css/MyStyleSheet.css" type="text/css" rel="stylesheet">
<title>Login Success</title>
</head>
<body>
<h1>
Login Success
</h1>
</body>
</html>
Register.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String context = request.getContextPath();
request.setAttribute("context", context);
%>
<html>
<head>
<link href="${pageContext.session.getAttribute("filePath")}/css/MyStyleSheet.css" type="text/css" rel="stylesheet">
<title>Register</title>
</head>
<body>
<h1>
This is Register Page
</h1>
<form action="${context}/UserController/Register/DoRegister" method="post">
<label>用户名:</label>
<input type="text" name="username" value="${requestScope.username}">
<br>
<label>密码</label>
<input type="password" name="password" value="${requestScope.password}">
<br>
<label>重复密码:</label>
<input type="password" name="repeatPassword" value="${requestScope.repeatPassword}">
<br>
<label style="color: red">
${requestScope.RegisterMessage}
</label>
<br>
<button type="submit">注册</button>
</form>
</body>
</html>
因为控制器的返回值是视图,所以要这样访问,根据控制器的标注修改 url ,如果只是 html + js 的话,可以直接 Views/
访问视图