更简单的清晰的理解各个框架之间的运行结构,通过一个简单的用户登录项目进行实现。首先项目用到的MyEclipse,Mysql,Tomcat。我们前面知道当一个url地址被写时,是从项目的web.xml进行开始的所以本项目的web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.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>
所以当我们在浏览器中输入http://localhost:8080/ThridProject时候,浏览器会进行解析发现了Struts的过滤器,进行过滤filter,这个时候会去struts.xml查看是否存在命名空间为“/"的默认action类型,而Struts..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 name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />
<package name="default" extends="struts-default" namespace="/">
<action name="registerAction" class="com.thridProject.action.RegisterAction">
<result type="dispatcher" name="true">/registerSuccess.jsp</result>
<result name="false">/registerFail.jsp</result>
</action>
</package>
可以看到没有默认的action只有registerAction故在翻译web.xml这个时候发现了welcome-file-list,故跳转到index.JSP而index.jsp的实现如下:
<form name="registerForm" action="registerAction" method="post">
<table id="registerTable" width="50%" border="1" align="center">
<tr align="center" bgcolor="#666666">
<td colspan="2">欢迎用户登录</td>
</tr>
<tr align="right">
<td>用户名:</td>
<td align="left"><input type="text" name="user.username" size="20"/></td>
</tr>
<tr align ="right" bgcolor="#666666">
<td>密 码:</td>
<td align="left"><input type="password" name="user.password" size="20"/></td>
</tr>
<tr align ="center">
<td colspan="2">
<input type="submit" name="submit" value="提交"/><a href="/register.jsp"><font color="red">注册</font></a>
</td>
</tr>
</table>
</form>
故运行会在浏览器他会显示如下:
当我们进行输入用户名和密码后点击提交,就会跳转到http://localhost:8080/ThridProject/registerAction而这个时候就会在Struts.xml的registerAction,并且执行action类的execute方法,RegisterAction的源码实现如下,我们使用DTO的方式进行参数传递:
package com.thridProject.action;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.opensymphony.xwork2.ActionSupport;
import com.thridProject.model.User;
public class RegisterAction extends ActionSupport {
/**
* author 张春蕾 2013-11-17
*/
private static final long serialVersionUID = 1L;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute(){
String strFinal = "false";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ThridProject","root","root");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from t_user where username='"+user.getUsername()+"'");
if(rs.next()){
if(rs.getString("password").equals(user.getPassword())){
strFinal = "true";
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strFinal;
}
}
我们看到在action类中用到了user类的实现user类的代码如下:
package com.thridProject.model;
/**
* author 张春蕾 2013年11-17
*/
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;
}
}
我们可以看到在execute方法中用到了JDBC进行数据库访问,对传入的数据进行查找,看这个用户是否存在,若存在看是否password是否正确。数据库的设计为:
CREATE DATABASE THRIDPROJECT;
USE THRIDPROJECT;
CREATE TABLE T_USER(
USERNAME VARCHAR(20) PRIMARY KEY,
PASSWORD VARCHAR(20)
);
INSERT INTO T_USER(USERNAME,PASSWORD) VALUES('ZHANG','123');
INSERT INTO T_USER(USERNAME,PASSWORD) VALUES("WANG","12345");
我们在原始的数据库中插入了两个数据,当我们输入的用户名是这两个中的一个,如输入ZHANG,123,在执行完execute方法后,strFinal为true就会根据Struts.xml跳转到registerSuccess.jsp,否则则跳转到registerFail.JSP.
登录成功的界面: