<%@ 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+"/";
%>
<HTML>
<HEAD>
<TITLE>管理中心登陆</TITLE>
<base href="<%=basePath%>">
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<LINK href="css/admin.css" type="text/css" rel="stylesheet">
</HEAD>
<BODY>
<s:form action="login.action" name="form1" method="post">
<TD>用户名</TD>
<TD>
<s:textfield name="user.username" label="名字" cssStyle="BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #000000 1px solid" maxLength="30" size="24" ></s:textfield>
<TD>口 令</TD>
<s:password name="user.password" label="密码" cssStyle="BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #000000 1px solid" maxLength="30" size="24" ></s:password> <s:submit></s:submit></TD>
</s:form>
|
注意:这里面的UI标签我们使用了cssStyle来定义其样式的!
2.编写POJO类
package com.date.po;
public class User {
private int id;
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;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
|
3.编写DAO类封装当前POJO的全部数据库操作
package com.date.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.date.db.ConMysql;
import com.date.po.User;
public class UserDao {
/**===========================
* 返回一个用户对象(登录验证)
* 参数列表:
* @param:username
* @param:passwor
* @return:
* 符合条件返回一个用户对象保存其属性
* 不符合条件返回的用户对象其属性为NULL
* */
public User getUser(String username,String password) {
User user = new User();
String sql = "select id,username,password from user where username=? and password=? limit 1";
PreparedStatement pstat = null;
ResultSet rs = null;
Connection connection = ConMysql.getConn();
try {
pstat = connection.prepareStatement(sql);
pstat.setString(1, username);
pstat.setString(2, password);
rs = pstat.executeQuery();
while (rs.next()) {
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (connection != null)
connection.close();
if (pstat != null)
pstat.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return user;
}
}
|
4.编写Action
package com.date.action;
import com.date.dao.UserDao;
import com.date.po.User;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private User user = null;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public void validate() {
//校验器在执行excute之前调用它的。这个其实跟servlets里面的验证器是一个道理
if (null == user.getUsername()||"".equals(user.getUsername().trim())) {
this.addFieldError("user.username", "userName is required");
} else if (null == user.getPassword()||"".equals(user.getPassword().trim())) {
this.addFieldError("user.password", "userPassword is required");
}
}
public String execute() throws Exception{ user = new UserDao().getUser(user.getUsername(), user.getPassword()); if (user.getUsername() == null) { this.addFieldError("user.username", "userName or password is wrong!"); return "failer"; } else { HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); session.setAttribute("username", user.getUsername()); return "success"; } }
}
说明:看一下如何使用好SESSION啊!在structs里面使用session
|
5.定义配置文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"">
<struts>
<include file="struts-default.xml"></include>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<!-- 控制编码 无需过滤器 也可以在struts.properties文件中控制且后者权限高-->
<package name="mldn" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"></interceptor>
<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
</interceptors>
<action name="login" class="com.date.action.LoginAction">
<result name="input">login.jsp</result>
<!-- 它具备了自动校验功能如果输入有误的话可以自动返回!当校验器一失败直接调用它返回的 -->
<result name="success">index.jsp</result>
<result name="failer">login.jsp</result>
</action>
</package>
</struts>
|
说明:
<result name="input">login.jsp</result>主要解决的问题是什么呢?
就是当我们的校验器校验的时候直接返回到指定的uri处!
this.addFieldError("user.username", "userName is required"); 注意这里面的
addFieldError("表单名称","提示性的语句");
其中表单名称是与HTML表单里面的字段名称保持一致的!
阅读(1333) | 评论(0) | 转发(0) |