1.下载支持Netbeans7.0的Struts2插件,在工具-插件-已下载-添加插件菜单下安装struts2插件。
2.新建一个web项目,在步骤4中选择struts2选项,去掉“Create example page”选项:
在自动生成web.xml中,给出了struts2过滤器配置,默认拦截所有请求:“\*”,
<filter>
<filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
改成只拦截“*.action”和“*.jsp”,即只对struts和jsp文件拦截:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
在自动生成的struts.xml文件(位于缺省包中)里,加入如下代码:
<package name="book" extends="struts-default">
<action name="UserAction" class="book.action.UserAction">
<result name="msg">/index.jsp</result>
</action>
</package>
表示凡是经过struts2过滤的名为“UserAction”的action,均执行“book.action.UserAction”类程序,根据结果转向“index.jsp”页面。
3.创建如图所示的包结构:
4.在“bbok.common”包放置工具类用于数据库连接,在此添加DB_Conn.java文件:
package book.common;
import java.io.PrintWriter;
import java.sql.*;
public class DB_Conn {
private String driverName="com.mysql.jdbc.Driver"; //驱动程序名
private String userName="root"; //数据库用户名
private String userPwd="123456"; //密码
private String url="jdbc:mysql://localhost:3306/book"; //连接字符串
private Connection conn=null; //数据库连接对象
public Statement sm=null; //数据库语句对象
private PrintWriter out = null;
// 建立数据库连接
public void ConnectDB(){
try {
Class.forName(driverName).newInstance();
conn=DriverManager.getConnection(url,userName,userPwd); //获得数据库连接
sm=conn.createStatement();
} catch(Exception e) {
out.print("数据库连接失败!");
}
}
public void CloseDB(){
try {
if (sm != null) {
sm.close();
}
conn.close();
} catch (SQLException SqlE) {
out.print("数据库关闭失败!");
}
}
}
5.包“book.model”放置实体类,在此添加user.java文件:
package book.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;
}
}
6.包“book.idao”放置数据访问接口,添加IUserDao接口文件:
package book.idao;
import book.model.User;
public interface IUserDao {
User fineOne(String userName,String userPassword);
}
7. 包“book.dao”放置数据访问类,添加UserDao.java文件,实现IUserDao接口,完成数据访问:
package book.dao;
import book.common.DB_Conn;
import book.model.User;
import book.idao.IUserDao;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao implements IUserDao{
//创建公用数据库连接类对象
DB_Conn db_conn=new DB_Conn();
//通过用户名称获取用户信息
@Override
public User fineOne(String username, String password) {
db_conn.ConnectDB();
ResultSet rs = null;
try {
String sql = "select * from admin where u_Name ='"
+ username + "' and u_Pass='" + password + "'";
rs = db_conn.sm.executeQuery(sql); // 执行sql语句
User user = null;
if (rs.next()) {
user = new User();
user.setUsername(username);
user.setPassword(rs.getString("u_Pass"));
return user;
}
return user;
}catch (SQLException SqlE) {
return null;
}catch (Exception E) {
return null;
}finally {
// 关闭连接,释放数据库资源:
db_conn.CloseDB();
}
}
}
8.在包“book.iservice”中放置数据的逻辑访问接口,在此添加IUserService.java接口文件:
package book.iservice;
import book.model.User;
public interface IUserService {
User findUserByNameAndPassword(String userName,String userPassword);
}
9. 包“book.service”放置数据逻辑访问类,添加UserService.java文件,实现IUserService,调用UserDao类的功能,完成对数据逻辑访问:
package book.service;
import book.iservice.IUserService;
import book.model.User;
import book.idao.IUserDao;
import book.dao.UserDao;
public class UserService implements IUserService{
private IUserDao userDao=new UserDao();
@Override
public User findUserByNameAndPassword(String userName,String userPassword){
return userDao.fineOne(userName, userPassword);
}
}
10.在“book.action”包中放置action类,以实现对系统的控制,在此添加UserAction.java文件,该action类调用数据的逻辑访问类UserService实现对数据库的查询,根据查询结果构造不同的字符串:
package book.action;
import com.opensymphony.xwork2.ActionContext;
import book.iservice.IUserService;
import book.service.UserService;
import book.model.User;
public class UserAction {
private IUserService userService=new UserService();
private String userName;
private String userPassword;
private String msg;
public String execute()throws Exception{
User user=userService.findUserByNameAndPassword(userName, userPassword);
if(user!=null){
msg="登录成功!"+user.getUsername();
ActionContext.getContext().getSession().put("user",user);
}else{
msg="登录失败!<a href=\"JavaScript:window.history.back()\">返回</a>";
}
return "msg";
}
public void setUserName(String userName){
this.userName=userName;
}
public void setUserPassword(String userPassword){
this.userPassword=userPassword;
}
public String getMsg(){
return msg;
}
}
11. 创建如下两个页面:
Login.jsp:
<%@ page pageEncoding="gb2312" %>
<%@ page contentType="text/html; charset=gb2312" %>
<% request.setCharacterEncoding("gb2312"); %>
<%@ page language="java" %>
<html>
<head><title>登录网页</title></head>
<body>
<form method="POST" action="UserAction.action">
<table >
<tr>
<td width="890" align="left" colspan="2">
<b><font size="5">系统登录</font></b>
<hr>
</td>
</tr>
<% if (request.getAttribute("error") != null){ %>
<tr>
<td width=100% align=center colspan=4 height=36>
<font color=red><b><%=request.getAttribute("error")%></b></font>
</td>
</tr>
<%
}
%>
<tr>
<td width="75" align="center">
<p align="right">账号:</p>
</td>
<td width="809" align="center">
<p align="left">
<input type="text" name="userName" size="20"></td>
</tr>
<tr>
<td width="75" align="center">
<p align="right">密码:</p>
</td>
<td width="809" align="center">
<p align="left">
<input type="password" name="userPassword" size="20">
<input type="submit" value="登录" name="loginButton"></td>
</tr>
</table>
</form>
</body>
</html>
在这个页面中,下面的form标记语句:
<form method="POST" action="UserAction.action">
表示该页面提交一个名为:"UserAction"的action,注意和struts.xml文件中的action的name相对应。
index.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>显示信息</title>
</head>
<body>
<h1>Hello World!</h1>
<div id="content">
<center>
<br/><%=request.getAttribute("msg")%>
</center>
</div>
</body>
</html>
该页面的标记语句:
<%=request.getAttribute("msg")%>
表示接收来自action的msg字符串。
12. 生成项目,并运行http://localhost:8080/login/login.jsp