这里省略登录界面和登录成功的跳转界面;这两个界面都是比较简单的jsp页面;省略了bean文件,其中只是封装了username和password属性
1.web.xml 配置文件
2.struts.xml配置文件,struts2配置最重要的文件,名字不能修改 struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* index.jsp 3. c3p0-config.xmlc 3p0配置文件 /sc.jsp /index.jsp 4.dao实现类package www.csdn.dao;import java.sql.SQLException;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;import www.csdn.domain.User;import com.csdn.util.DBManager;public class ActionDao { public User login(String username,String password)throws SQLException{ QueryRunner qr = new QueryRunner(DBManager.getDataSource()); String sql = "select * from login where username = ? and password = ?"; Object[] params = {username,password}; User user = (User) qr.query(sql,params,new BeanHandler(User.class)); return user; }}5.action主要类package www.csdn.struts2.demo;import java.sql.SQLException;import www.csdn.dao.ActionDao;import www.csdn.domain.User;import com.opensymphony.xwork2.ActionSupport;/*** * * 自己编写的action类可以不继承ActionSupport,不继承直接使用自己编写的类不会出错; * 但是大多数情况下要继承ActionSupport类或实现Action接口,因为ActionSupport类中封装了大多数需要的常量, * 继承之后就可以直接使用这些常量而不需要再自己去定义 */public class DemoAction extends ActionSupport{ //封装请求的参数,必须与jsp页面中的input标签的username名字相同 private String username; private String password; private User entity; //set方法用于获取jsp页面传过来的参数 public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public User getEntity() { return entity; } //get方法用于从action中获取值传到web的jsp页面输出 /* public String getUsername() { return username; }*/ public String login(){ ActionDao dao = new ActionDao(); try { entity = dao.login(username,password); } catch (SQLException e) { e.printStackTrace(); } if(entity != null){ return SUCCESS; }else{ return LOGIN; } } public String execute(){ System.out.println("处理中。。。。。。"); return SUCCESS; }}6.util工具类package com.csdn.util;import java.sql.Connection;import java.sql.SQLException;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class DBManager { private static ComboPooledDataSource ds = null; static{ //这里要注意的是:xml配置文件的名字一定要是c3p0-config.xml,可以doc参考帮助,帮助中有命名,如果名字写错了会无法加载驱动,空指针的错 ds = new ComboPooledDataSource("mysql"); } public static Connection getCon() throws SQLException{ return ds.getConnection(); } public static DataSource getDataSource(){ return ds; }} com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/struts2 root root 10 30 100 10 200 com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/struts2 root root 5 10 50 1000