做一个不带拦截器的struts 登录验证 不带拦截器,用户能直接访问action页面访问到数据,起不到登录验证的作用
1.使用c3p0 连接 mysql ,数据库字段与 java 字段一致
c3p0默认加载 c3p0-config.xml 配置文件
<c3p0-config> <default-config> <property name="jdbcUrl">jdbc:mysql:///hib_demo </property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">root</property> <property name="initialPoolSize">3</property> <property name="maxPoolSize">6</property> <property name="maxIdleTime">1000</property> </default-config> <named-config name="oracle_config"> <property name="jdbcUrl">jdbc:mysql:///hib_demo</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">root</property> <property name="initialPoolSize">3</property> <property name="maxPoolSize">6</property> <property name="maxIdleTime">1000</property> </named-config> </c3p0-config>
jdbcutils.java cn.itcast.utils包
package cn.itcast.utils; import javax.sql.DataSource; import org.apache.commons.dbutils.QueryRunner; import com.mchange.v2.c3p0.ComboPooledDataSource; public class jdbcutils { private static DataSource datasource; static{ datasource =new ComboPooledDataSource(); } public static DataSource getdatasource(){ return datasource; } public static QueryRunner getquerrrunner(){ return new QueryRunner(datasource); } }
=====================================================================
2.项目中加载的jar包:
struts-jar( commons-fileupload-1.2.2;commons-io-2.0.1;commons-lang3-3.1;freemarker-2.3.19;javassist-3.11.0.GA;ognl-3.0.5;struts2-core-2.3.4.1;xwork-core-2.3.4.1)
c3p0-0.9.1.2;commons-dbutils-1.6;mysql-connector-java5.1.12
3. login.jsp 登录页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <form method="post" action="${pageContext.request.contextPath }/user_login.action"> 用户名:<input type="text" name="admin.username"><br/> //存放值 密码:<input type="text" name="admin.pwd"><br/> <input type="submit" value="登陆"><br/> </form> </body> </html>
list.jsp 显示页面 位置放置于 webrootweb-inf
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>list</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<h1>欢迎你,${userinfo.username }</h1>
<table align="center" border="1">
<tr>
<td>序号</td>
<td>编号</td>
<td>用户名</td>
<td>密码</td>
</tr>
<%@taglib uri="/struts-tags" prefix="s" %>
<s:iterator var="admin" value="#request.listadmin" status="st">
<tr>
<td>
<s:property value="#st.count"/> //ognl表达式取值
</td>
<td>
<s:property value="#admin.id"/>
</td>
<td>
<s:property value="#admin.username"/>
</td>
<td>
<s:property value="#admin.pwd"/>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>
web.xml struts拦截所有请求
<?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"> <display-name></display-name> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
4. admin.java 跟数据库 字段一致
package cn.itcast.entity; public class admin { private int id; private String username; private String pwd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
admindao.java
package cn.itcast.dao; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.junit.Test; import cn.itcast.entity.admin; import cn.itcast.utils.jdbcutils; public class admindao { public admin login(admin admin){ String sql="select * from admin where username=? and pwd=?"; try { return jdbcutils.getquerrrunner().query(sql,new BeanHandler<admin>(admin.class),admin.getUsername(),admin.getPwd()); } catch (SQLException e) { // TODO Auto-generated catch block throw new RuntimeException(e); } } public List<admin> getall(){ String sql="select * from admin"; try { return jdbcutils.getquerrrunner().query(sql, new BeanListHandler<admin>(admin.class)); } catch (SQLException e) { // TODO Auto-generated catch block throw new RuntimeException(e); } } }
adminservice.java
package cn.itcast.service; import java.util.List; import cn.itcast.dao.admindao; import cn.itcast.entity.*; public class adminservice { private admindao admindao = new admindao(); public admin login(admin admin){ return admindao.login(admin); } public List<admin> getall(){ return admindao.getall(); } }
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> <package name="user" extends="struts-default"> <global-results> <result name="error">/error.jsp</result> </global-results> <action name="user_*" class="cn.itcast.action.useraction" method="{1}"> <!-- 1. 登陆失败 --> <result name="input">/login.jsp</result> <!-- 2. 登陆成功 --> <result name="loginSuccess" type="redirectAction">user_list</result> //设置 跳转 而不是简单的页面跳转,需要传值! <!-- 3. 列表展示 --> <result name="list">/WEB-INF/list.jsp</result> </action> </package> </struts>
useraction.java
package cn.itcast.action; import java.util.List; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import cn.itcast.entity.*; import cn.itcast.service.*; public class useraction extends ActionSupport { private admin admin; public admin getAdmin() { //注意 如果该处写 getadmin() setadmin() ;struts是找不到admin数据的;受到struts取值影响,必须使用eclipse习惯用法。 return admin; } public void setAdmin(admin admin) { this.admin = admin; } private adminservice adminservice= new adminservice(); public String login(){ try { admin userinfo= adminservice.login(admin); if(userinfo==null){ return "input"; } ActionContext.getContext().getSession().put("userinfo", userinfo); return "loginSuccess"; } catch (Exception e) { // TODO Auto-generated catch block return ERROR; } } public String list(){ try { List<admin> list=adminservice.getall(); ActionContext.getContext().getContextMap().put("listadmin", list); return "list"; } catch (Exception e) { // TODO Auto-generated catch block return ERROR; } } }
设置拦截器:
UserCheckInterceptor.java
package cn.itcast.interceptor; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ActionProxy; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class UserCheckInterceptor extends AbstractInterceptor{ /** * 拦截器业务处理方法 */ public String intercept(ActionInvocation invocation) throws Exception { // 拿到当前执行的方法名:判断,只有当前方法名不是login,就进行验证 // 获取ActionContext对象 ActionContext ac = invocation.getInvocationContext(); // 获取action的代理对象 ActionProxy proxy = invocation.getProxy(); // 获取当前执行的方法名 String methodName = proxy.getMethod(); // 判断 if (!"login".equals(methodName)) { // 先获取当前登陆的用户 Object obj = ac.getSession().get("userInfo"); if (obj == null) { // 没有登陆 return "input"; } else { // 当前用户有登陆 return invocation.invoke(); } } else { // 说明当前用户正在登陆 return invocation.invoke(); } } }
à 配置拦截器 |
<?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> <package name="user" extends="struts-default">
<!-- 【拦截器配置】 --> <interceptors> <interceptor name="loginCheck" class="cn.itcast.interceptor.UserCheckInterceptor"></interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="loginCheck"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 【执行拦截器:第一种写法: 当前包下所有的acntion都执行myStack栈】 <default-interceptor-ref name="myStack"></default-interceptor-ref> -->
<!-- 全局配置 --> <global-results> <result name="error">/error.jsp</result> </global-results>
<action name="user_*" class="cn.itcast.action.UserAction" method="{1}">
<!--第二种写法: 只是在这一个Action中执行myStack栈 <interceptor-ref name="defaultStackt"></interceptor-ref> <interceptor-ref name="loginCheck"></interceptor-ref> -->
<!-- 第三种写法:执行用户栈(与第二种写法一样, 只在当前aciton中执行自定义栈) --> <interceptor-ref name="myStack"></interceptor-ref>
<!-- 1. 登陆失败 --> <result name="input">/login.jsp</result>
<!-- 2. 登陆成功 --> <result name="loginSuccess" type="redirectAction">user_list</result>
<!-- 3. 列表展示 --> <result name="list">/WEB-INF/list.jsp</result>
</action>
</package> </struts> |