都看一个星期struts了,做一个登录验证,刚有一点点入门,挺有意思的,加油吧,同志,睡觉~~~~~~~~~
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<?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="true" />
<constant name="struts.i18n.encoding" value="utf-8" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<!-- Add packages here -->
<!--include action验证用户登录xml -->
<include file="login.xml"></include>
<!-- 测试result的type烈性的片段 -->
<include file="result.xml"></include>
</struts>
<?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>
<package name="login" namespace="/login" extends="struts-default">
<action name="login"
class="com.struts.action.login.UserCheck">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
<action name="list"
class="com.struts.action.login.UserCheck" method="list">
<result>list.jsp</result>
</action>
</package>
</struts>
package com.struts.action.login;
import java.util.ArrayList;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class UserCheck extends ActionSupport implements SessionAware,
RequestAware {
private Map<String, Object> session;
private Map<String, Object> request;
private User user;
@Override
public String execute() throws Exception {
if (Domain.checkUser(user.getUsername()).equals(user.getPassword())) {
return SUCCESS;
} else {
if (Domain.checkUser(user.getUsername()).equals("")) {
this.addFieldError("error", "用户名【" + user.getUsername()
+ "】不存在");
} else if (!Domain.checkUser(user.getUsername()).equals(
user.getPassword())) {
this.addFieldError("error", "用户【" + user.getUsername()
+ "】的密码错误");
} else {
this.addFieldError("error", "用户名或者密码错误");
}
return ERROR;
}
}
public User getUser() {
return user;
}
public String list() throws Exception {
ArrayList<User> users = Domain.list();
request.put("users", users);
return SUCCESS;
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
public void setUser(User user) {
this.user = user;
}
}
package com.struts.action.login;
import java.sql.*;
import java.util.*;
public class DataBaseConnection
{
Properties pro = new Properties();
private Connection conn = null;
public DataBaseConnection()
{
try
{
pro.load(this.getClass().getClassLoader().
getResourceAsStream("dbc.properties"));
String DBDRIVER = pro.getProperty("driver");
String DBURL = pro.getProperty("url");
String DBUSER = pro.getProperty("db_name");
String DBPASSWORD = pro.getProperty("db_pass");
Class.forName(DBDRIVER) ;
this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
}
catch (Exception e)
{
e.printStackTrace();
}
}
public Connection getConnection()
{
return this.conn ;
}
public void close()
{
try
{
this.conn.close() ;
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
package com.struts.action.login;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class Domain {
public static String checkUser(String username) {
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "SELECT password FROM t_user WHERE username = '" + username+"'";
DataBaseConnection dbc = null;
dbc = new DataBaseConnection();
try {
pstmt = dbc.getConnection().prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getString(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static ArrayList<User> list() {
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "SELECT * FROM t_user";
DataBaseConnection dbc = null;
ArrayList<User> users = new ArrayList<User>();
dbc = new DataBaseConnection();
try {
pstmt = dbc.getConnection().prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
return users;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/login/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>struts用户登录</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" method="post">
用户名:
<input name="user.username" autocomplete="off" style="ime-mode: disabled" type="text" />
<br>
密 码:
<input name="user.password" type="password" />
<br>
<input name="登录" type="submit" />
<input name="重置" type="reset" />
<br>
</form>
</body>
</html>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/login/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>struts用户登录</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>
<h2>
<font color="red">success</font>
欢迎<font color="red"><s:property value="#session.user"/></font>登录
<a href="list">查看用户</a>
<s:debug></s:debug>
</h2>
</body>
</html>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/login/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>struts用户登录</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>
<h2>
用户列表:<br>
<s:iterator var="user" status="status" value="#request.users" >
<s:if test="#status.count == 7">
<font color="red">序号:<s:property value="#status.count"/>|用户名:<s:property value="#user.username"/>|密码:<s:property value="#user.password"/></font><br>
</s:if>
<s:else>
序号:<s:property value="#status.count"/>|用户名:<s:property value="#user.username"/>|密码:<s:property value="#user.password"/><br>
</s:else>
</s:iterator>
</h2>
</body>
</html>