效果图
文章目录
Login视图及功能实现
① 导入相关依赖(pom.xml文件)
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
②Main类
代码
start
方法
private final String APPLICATION_NAME = "xxx心信息系统";
private Stage primaryStage;
private JdbcTemplate jdbcTemplate;
@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
this.primaryStage.setTitle(APPLICATION_NAME);
// 设置程序运行的窗口的icon
this.primaryStage
.getIcons()
.add(new Image(Main.class.getClassLoader().getResourceAsStream("cn/oldsheep/javafx/view/images/xxxxx.png")));
showLoginView();
}
showLoginView方法
/**
* Initializes the login view
*/
public void showLoginView() {
try {
FXMLLoader loader = viewLoader("LoginView");
AnchorPane loginView = loader.load();
primaryStage.setScene(new Scene(loginView));
LoginViewController controller = loader.getController();
controller.setMainApp(this);
controller.setJdbcTemplate(jdbcTemplate);
primaryStage.setResizable(false); // 禁用最大化
//primaryStage.initStyle(StageStyle.UNDECORATED); // 没有最大化最小化及关闭按钮
primaryStage.show();
} catch (IOException e) {
AlertUtil.exceptionAlert(e);
}
}
viewLoader方法
/**
* Load overview.
* @param view the specified load view
* @return view's loader
*/
private FXMLLoader viewLoader(String view) {
StringBuffer sb = new StringBuffer("view/").append(view).append(".fxml");
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource(sb.toString()));
return loader;
}
③LoginViewController
代码
package cn.oldsheep.javafx.view;
import cn.oldsheep.javafx.Main;
import cn.oldsheep.javafx.dao.JdbcTemplate;
import cn.oldsheep.javafx.model.TBarAccount;
import cn.oldsheep.javafx.utils.AlertUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 登录视图控制器
* author : oldsheep
* qq|wechat: 64341393
* Create on 2020/12/27.
*/
public class LoginViewController {
private static Log LOGGER = LogFactory.getLog(LoginViewController.class);
@FXML
private PasswordField userPwd;
@FXML
private Button loginBtn;
@FXML
private TextField userAccount;
private Main mainApp;
private JdbcTemplate jdbcTemplate;
public void setMainApp(Main mainApp) {
this.mainApp = mainApp;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@FXML
private void initialize() {}
/**
* login the application
*/
@FXML
private void handleLoginApp() {
if (isInputValid()) {
final TBarAccount login = jdbcTemplate.login(userAccount.getText(), userPwd.getText());
if (null != login && 1 == login.getStatus()) {
// save login status
mainApp.setLoginUser(login);
mainApp.showMainView();
mainApp.showBarCodeView();
LOGGER.info("login successfully~!");
} else {
AlertUtil.errorAlert(new StringBuffer("请输入正确的用户名口令!"));
this.userAccount.setText(null);
this.userPwd.setText(null);
}
}
}
/**
* Validates the user input in text fields
* @return true if the input is valid
*/
private boolean isInputValid() {
StringBuffer sb = new StringBuffer();
if (userAccount.getText() == null || userAccount.getText().length() == 0) {
sb.append("请输入您的账号!\n");
} else if (userPwd.getText() == null || userPwd.getText().length() == 0) {
sb.append("请输入您的密码!\n");
}
if (sb.toString().length() != 0) {
AlertUtil.errorAlert(sb);
return false;
}
return true;
}
}
④JdbcTemplate
代码
package cn.oldsheep.javafx.dao;
import cn.oldsheep.javafx.model.TBarAccount;
import cn.oldsheep.javafx.model.TBarCode;
import cn.oldsheep.javafx.utils.C3P0Utils;
import cn.oldsheep.javafx.utils.DateUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
/**
* Created by oldsheep on 20/12/28.
*/
public class JdbcTemplate {
/* logger */
private static Log LOG = LogFactory.getLog(JdbcTemplate.class);
/**
* 用户账号登录操作
*/
public TBarAccount login(String userAccount, String pwd) {
try {
final QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
String sql = "select * from tb_barAccount where userAccount=? and pwd=? and status = 1";
final List<TBarAccount> user = runner.query(sql,
new BeanListHandler<>(TBarAccount.class), userAccount, DigestUtils.md5Hex(pwd));
if (user != null && user.size() == 1) {
return user.get(0);
}
return null;
} catch (SQLException e) {
// e.printStackTrace();
LOG.error(e.getMessage() + e.fillInStackTrace());
return null;
}
}
}
⑤TBarAccount
代码
package cn.oldsheep.javafx.model;
/**
* Created by oldsheep on 20/12/28.
*/
public class TBarAccount {
private String userAccount;
private String pwd;
private int status;
public TBarAccount() {
}
public TBarAccount(String userAccount, String pwd, int status) {
this.userAccount = userAccount;
this.pwd = pwd;
this.status = status;
}
public String getUserAccount() {
return userAccount;
}
public void setUserAccount(String userAccount) {
this.userAccount = userAccount;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}