使用struts2建一个简单的登录页面。
首先配置struts.xml,如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<!-- Add packages here -->
<package name="user" extends="struts-default" >
<action name="login" class="com.login.action.LoginAction" method="login">
<result name="success">/loginsuccess.jsp</result>
<result name="input">/loginfail.jsp</result>
</action>
</package>
</struts>
写对应的action,在src目录下建一个包,名为com.login.action,在里面建立一个类
public class LoginAction extends ActionSupport{
User u;
UserService us = new UserService();
public User getU() {
return u;
}
public void setU(User u) {
this.u = u;
}
public UserService getUs() {
return us;
}
public void setUs(UserService us) {
this.us = us;
}
public String login(){
if(us.login(u))
return SUCCESS;
else
return INPUT;
}
}
Com.login.db下的类如下:
public class DB {
public static Connection getCon(){
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/login?user=root&password=root";
con=DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public static PreparedStatement prepare(Connection con,String sql){
PreparedStatement ps=null;
try {
ps=con.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return ps;
}
public static void close(Connection con){
try {
if(con != null) {
con.close();
con = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement stmt){
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs){
try {
if(rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Com.login.model下的类如下:
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;
}
}
com.login.service下的类如下:
public class UserService {
public boolean login(User u){
Connection con = DB.getCon();
String sql = "select * from login where username='+u.getUsername()+' and password='+u.getPassword()+'";
PreparedStatement ps = DB.prepare(con, sql);
try {
ResultSet rs = ps.executeQuery();
if(rs.next())
return true;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
相应的index.jsp文件如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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="login.action">
username:<input name="u.username" type="text" size=15>
password:<input name="u.password" type="password" size=15>
<input type="submit" value="login">
<input type="reset" value="reset">
</form>
</body>
</html>
loginfail.jsp如下:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>My JSP 'index.jsp' starting page</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>
login fail! <br>
</body>
</html>
loginsuccess.jsp如下:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>My JSP 'index.jsp' starting page</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>
login success! <br>
</body>
</html>
如将以上文件拷贝到你的tomcat的webapps目录下面,部署好,如果不会配置,请看我的笔记一,然后启动你的tomcat。
如果登录不上,请读者自己修改db类中的数据库连接的语句,与你自己所在的机子的mysql密码,以及数据库想匹配上才能够正确登录。