第一步:配置struts2,包括导入jar包,配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant value="false" name="struts.enable.DynamicMethodInvocation"/>
<constant value="true" name="struts.devMode"/>
<package name="default" namespace="/" extends="struts-default">
<action name="LoginAction" method="login" class="com.xatu.LoginAction">
<result>/success.jsp</result>
<result name="input">/register.jsp</result>
</action>
<action name="RegisterAction" method="register" class="com.xatu.RegisterAction">
<result>/relogin.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<filter>
<filter-name>Login</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Login</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
第二部:各部分代码实现
①数据库连接
package com.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Database {
private String dbDriver = "com.mysql.jdbc.Driver";
//数据库名struts2,表名user
private String dbUrl = "jdbc:mysql://localhost:3306/struts2?useUnicode=true&characterEncoding=utf8";
private String dbUser="root";
private String dbPass="mysql";
private Connection con;
public Connection getcon(){
try {
Class.forName(dbDriver);
con = DriverManager.getConnection(dbUrl,dbUser,dbPass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
}
②javabean
package com.po;
public class User {
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;
}
}
③实现登录注册的action类
package com.xatu;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import com.db.Database;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.po.User;
@SuppressWarnings("serial")
public class RegisterAction extends ActionSupport implements ModelDriven<User>{
private User user = new User();
public String register(){
try {
Connection con = new Database().getcon();
Statement st = con.createStatement();
String sql = "insert into user values ('"+user.getUsername()+"','"+user.getPassword()+"')";
int i = st.executeUpdate(sql);
if(i>0){
return SUCCESS;
}else{
return INPUT;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
package com.xatu;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.db.Database;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.po.User;
@SuppressWarnings("serial")
public class LoginAction extends ActionSupport implements ModelDriven<User>{
private User user = new User();
public String login(){
try {
Connection con = new Database().getcon();
Statement st = con.createStatement();
String sql = "select * from user";
ResultSet rs = st.executeQuery(sql);
boolean f = true;
while(rs.next()){
String username = rs.getString(1);
String password = rs.getString(2);
if(user.getUsername().equals(username) && user.getPassword().equals(password)){
f = false;
return SUCCESS;
}
}
if(f = true){
return INPUT;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public User getModel() {
return user;
}
}
第三部分:jsp页面
<%@ page language="java" import="java.util.*,com.po.User" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>注册页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="RegisterAction" method="post">
用户名:<input type="text" name="username"><br>
密 码:<input type="password" name="password"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
<%@ page language="java" import="java.util.*,com.po.User" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登陆成功页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
恭喜你,${username}登录成功!
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>注册成功,重新登录</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="login.jsp">注册成功,点击登录!</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登录页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="LoginAction" method="post">
用户名:<input type="text" name="username"><br>
密 码:<input type="password" name="password"><br><s:fielderror name="username"></s:fielderror>
<input type="submit" value="登录" />
</form>
</body>
</html>
Struts2也是基于MVC设计模式的一个java框架,本次项目中也是按照MVC设计模式设计的。