Struts(二)
=============================基本结构=============================
登录程序运行流程图:
login.jsp-->核心控制器-->Action-->Result-->success.jsp
核心控制器:
1.在web.xml中进行配置
2.对框架进行初始化,以及处理所有的请求
Action:
1.开发Action
a.实现Action接口
b.继承ActionSupport类
范例:
<struts>
<package name="default" namespace="/" extends="struts-default">
<!-- 请求地址/需要调用的Servlet -->
<action name="login" class="com.xuetamg9.demo.action.LoginAction">
<result name="success">/page/manage.jsp</result>
<result name="input">/page/login.jsp</result>
<result name="error">/page/error.jsp</result>
</action>
</package>
</struts>
Result:
1.实现对结果的调用
2.result元素的值指定对应的实际资源位置
3.name属性表示result逻辑名
结果类型:
a.登录成功,重定向到index.action
b.验证失败,转发到登录页面
c.登录失败,重定向到错误页面
范例:
1 <struts> 2 <package name="default" namespace="/" extends="struts-default"> 3 <action name="login" class="com.xuetamg9.demo.action.LoginAction" method="login"> 4 <!-- 登陆成功 --> 5 <result name="success" type="redirectAction">index.action</result> 6 7 <!-- 验证失败 --> 8 <result name="input" type="dispatcher">/login.jsp</result> 9 10 <!-- 登陆失败 --> 11 <result name="error" type="redirect">/page/error.jsp</result> 12 </action> 13 </package> 14 </struts>
==================================配置文件=============================
一、struts.xml
1.核心配置文件,主要负责管理Action
2.通常放在WEB-INF/classes目录下,在该目录下的struts.xml文件可以被自动加载
范例:
1 <struts> 2 <constant name="" value=""/> 3 <package name="" namespace="" extends=""> 4 <action name="" class=""> 5 <result name=""></result> 6 </action> 7 </package> 8 </struts>
a.constant元素
1.配置常量,可以改变Struts2框架的一些行为
2.name属性表示常量名称,value属性表示常量值
范例:
1 <struts> 2 <constant name="struts.i18n.encoding" value="UTF-8"/> 3 <package name="" namespace="" extends=""> 4 <action name="" class=""> 5 <result name=""></result> 6 </action> 7 </package> 8 </struts>
b.package元素
1.包的作用:简单维护工作,提高量用性,可以"继承"包,并添加自己包的配置
2.name属性必须唯一,指定包的名称
3.extends属性指要继承的包
4.namespace属性定义该包中action的命名空间,为可选属性
范例:
1 <struts> 2 <constant name="" value=""/> 3 <package name="default" namespace="/" extends="struts-default"> 4 <action name="" class=""> 5 <result name=""></result> 6 </action> 7 </package> 8 </struts>
二、struts-default.xml
1.Struts2默认配置文件,会自动加载
2.struts-default包在struts-default.xml文件中定义
三、struts-plugin.xml
1.Struts2插件使用的配置文件
加载顺序:struts-default.xml --> struts-plugin.xml --> struts.xml
==================================Action=============================
作用:
1.封装工作单元
2.数据转移场所
3.返回(字符串)结果逻辑名(execute方法)
范例:
1 public class HelloWorldAction implements Action { 2 private String name = ""; 3 private String message = ""; 4 public String execute() { 5 this.setMessage("Hello,"+this.getName()+"!"); 6 return SUCCESS; 7 } 8 //...省略setter/getter方法 9 }
属性:
1.method
a.实现Action中不同方法的调用
b.特点:
1)避免动态方法调用的安全隐患
2)导致大量的Action配置
范例:
1 <action name="login" class="com.xuetang9.demo.action.UserAction" method="login"> 2 ………… 3 </action> 4 <action name="register" class="com.xuetang9.demo.action.UserAction" method="register"> 5 ………… 6 </action>
动态方法调用:
1.作用:减少Action数量
2.使用:actionName!methodName.action
3.禁用:将属性struts.enable.DynamicMethodInvocation设置为false
范例:/user!login.action --> 调用UserAction的login()方法
1 public class UserAction implements Action { 2 … 3 public String login() {… } 4 public String register() {…} 5 }
1 <action name="user" class="com.xuetang9.demo.action.UserAction"> 2 <result name="login">/page/success.jsp</result> 3 …… 4 </action>
通配符(*)的使用:另一种形式的动态方法调用
范例:
1 <action name= "*User" class="com.xuetang9.demo.action.UserAction" method="{1}"> 2 <result>/page/{1}_success.jsp</result> 3 <result name="input">/page/{1}.jsp</result> 4 </action>
/loginUser.action --> method="login"
/page/login_success.jsp
/page/login.jsp
/registerUser.action --> method= "register"
/page/register_success.jsp
/page/register.jsp
配置默认Action:
1.没有Action匹配请求时,默认Action将被执行
2.通过<default-action-ref … />元素配置默认Action
范例:
1 <struts> 2 <package name="default" extends="struts-default"> 3 <default-action-ref name="defaultAction"/ > 4 <!-- 省略class属性,将使用ActionSupport类 --> 5 <action name="defaultAction"> 6 <result>error.jsp</result> 7 </action> 8 </package> 9 </struts>
【注意:如果请求的Action不存在,页面跳转到error.jsps】
====================================Result===================================
常用结果类型:
1.dispatcher类型(转发)
默认结果类型,后台使用RequestDispatcher转发请求
2.redirect类型(重定向jsp)
后台使用的sendRedirect()将请求重定向至指定的URL
3.redirectAction类型(重定向Action)
主要用于重定向到Action
配置动态结果--动态结果
配置时不知道执行后的结果是哪一个,运行时才知道哪个结果作为视图显示给用户
范例:
1 public class UserAction extends ActionSupport { 2 private String nextDispose; 3 public String login() { 4 ... 5 if(user.isManager()){ 6 nextDispose = "manager"; 7 }else{ 8 nextDispose = "common"; 9 } 10 return SUCCESS; 11 } 12 13 public String getNextDispose(){ 14 return nextDispose; 15 } 16 }
1 <struts> 2 <package name="default" extends="struts-default"> 3 <action name="login" class="com.xuetang9.demo.action.UserAction" method="login"> 4 <result type="redirectAction">${nextDispose}</result> 5 <result name="error">/page/error.jsp</result> 6 </action> 7 <action name="common" class="com.xuetang9.demo.action.CommonUserAction"> 8 … 9 </action> 10 <action name="manage" class="com.xuetang9.demo.action.ManagerAction"> 11 ... 12 </action> 13 </package> 14 </struts>
============================全局结果==========================
实现同一个包中多个action共享一个结果
范例:
1 <struts> 2 3 <package name="default" extends="struts-default"> 4 <default-action-ref name="defaultAction"/ > 5 6 <!-- 全局结果位于package元素内--> 7 <global-results> 8 <result name="error">/error.jsp</result> 9 <result name="login" type="redirect">/login.jsp</result> 10 </global-results> 11 12 …省略action的配置… 13 </package> 14 </struts>
范例:
1.实体类及其配置文件
a.User类
1 package com.Elastic.StrutsDemo2.ivy.entity; 2 3 import java.io.Serializable; 4 public class User implements Serializable{ 5 private String userName; 6 private String passWord; 7 8 public String getUserName() { 9 return userName; 10 } 11 public void setUserName(String userName) { 12 this.userName = userName; 13 } 14 public String getPassWord() { 15 return passWord; 16 } 17 public void setPassWord(String passWord) { 18 this.passWord = passWord; 19 } 20 }
b.User.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping> 5 <class name="com.Elastic.StrutsDemo2.ivy.entity.User" table="user"> 6 <id name="userName" type="java.lang.String"> 7 <column name="userName"></column> 8 <generator class="assigned"></generator> 9 </id> 10 <property name="passWord" type="java.lang.String"> 11 <column name="passWord"></column> 12 </property> 13 </class> 14 </hibernate-mapping>
2.Hibernate配置文件 -- hibernate.cfg.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE hibernate-configuration PUBLIC 4 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 5 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 6 7 <hibernate-configuration> 8 <session-factory> 9 <!-- 1.数据库连接信息 --> 10 <property name="connection.url">jdbc:mysql://localhost/hibernatedb</property> 11 <property name="connection.username">root</property> 12 <property name="connection.password">root</property> 13 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 14 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 15 <property name="show_sql">true</property> 16 <property name="format_sql">true</property> 17 18 <!-- 2.使用c3p0连接池 --> 19 <property name="hibernate.connection.datasource">com.mchange.v2.c3p0.ComboPooledDataSource</property> 20 <property name="c3p0.max_size">100</property> 21 <property name="c3p0.min_size">10</property> 22 <property name="c3p0.acquire_increment">5</property> 23 <property name="c3p0.idle_test_period">60</property> 24 <property name="c3p0.timeout">10</property> 25 26 <!-- 3.数据库对应的实体类的映射文件路径 --> 27 <mapping resource="com/Elastic/StrutsDemo2/ivy/entity/User.hbm.xml"></mapping> 28 </session-factory> 29 </hibernate-configuration>
3.util包 -- HibernateUtil类
1 package com.Elastic.StrutsDemo2.ivy.util; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.Configuration; 6 public final class HibernateUtil { 7 private static Configuration cfg = null; 8 private static SessionFactory sessionFactory = null; 9 10 //本地线程 11 public static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); 12 13 static{ 14 cfg = new Configuration().configure(); 15 sessionFactory = cfg.buildSessionFactory(); 16 } 17 18 public static Session getSession(){ 19 Session session = threadLocal.get(); 20 if (null == session || !session.isOpen()) { 21 session = sessionFactory.openSession(); 22 threadLocal.set(session); 23 } 24 return session; 25 } 26 27 }
4.filter包 -- OpenSessionInViewFilter包
1 package com.Elastic.StrutsDemo2.ivy.filter; 2 3 import java.io.IOException; 4 import javax.servlet.Filter; 5 import javax.servlet.FilterChain; 6 import javax.servlet.FilterConfig; 7 import javax.servlet.ServletException; 8 import javax.servlet.ServletRequest; 9 import javax.servlet.ServletResponse; 10 import org.hibernate.HibernateException; 11 import org.hibernate.Session; 12 import org.hibernate.Transaction; 13 import com.Elastic.StrutsDemo2.ivy.util.HibernateUtil; 14 public class OpenSessionInViewFilter implements Filter{ 15 16 @Override 17 public void doFilter(ServletRequest request, ServletResponse response, 18 FilterChain chain) throws IOException, ServletException { 19 20 //获取session 21 Session session = HibernateUtil.getSession(); 22 Transaction tx = session.beginTransaction(); 23 24 try { 25 chain.doFilter(request, response); 26 // tx.commit(); 27 } catch (HibernateException e) { 28 tx.rollback(); 29 e.printStackTrace(); 30 }finally{ 31 session.close(); 32 } 33 } 34 35 @Override 36 public void destroy() { 37 } 38 39 @Override 40 public void init(FilterConfig arg0) throws ServletException { 41 } 42 }
5.dao包
a.IBaseDao接口
1 package com.Elastic.StrutsDemo2.ivy.dao; 2 3 import java.io.Serializable; 4 import java.util.List; 5 import java.util.Map; 6 7 import org.hibernate.Session; 8 import org.hibernate.criterion.DetachedCriteria; 9 public interface IBaseDao<T> { 10 /** 11 * 12 * <p> 13 * <h3>方法功能描述:获取Session对象</h3> 14 * </p> 15 * @return 16 * @procedure 执行过程 17 * @see IBaseDao 18 */ 19 Session getSession(); 20 21 /** 22 * 23 * <p> 24 * <h3>方法功能描述:保存数据</h3> 25 * </p> 26 * @param record 需要保存的对象 27 * @procedure 执行过程 28 * @see IBaseDao 29 */ 30 void save(T record); 31 32 /** 33 * 34 * <p> 35 * <h3>方法功能描述:根据主键删除对应的数据</h3> 36 * </p> 37 * @param id 38 * @procedure 执行过程 39 * @see IBaseDao 40 */ 41 //不明确id的类型,就用Serializable 42 void delete(Serializable id); 43 44 /** 45 * 46 * <p> 47 * <h3>方法功能描述:根据数据对象删除数据库中对应的数据</h3> 48 * </p> 49 * @param record 50 * @procedure 执行过程 51 * @see IBaseDao 52 */ 53 void delete(T record); 54 55 /** 56 * 57 * <p> 58 * <h3>方法功能描述:根据指定的对象修改对应的数据</h3> 59 * </p> 60 * @param record 61 * @procedure 执行过程 62 * @see IBaseDao 63 */ 64 void update(T record); 65 66 /** 67 * 68 * <p> 69 * <h3>方法功能描述:根据主键查询对应的数据</h3> 70 * </p> 71 * @param id 72 * @return 返回查找到的数据,如果没有返回null 73 * @procedure 执行过程 74 * @see IBaseDao 75 */ 76 T findById(Serializable id); 77 78 /** 79 * 80 * <p> 81 * <h3>方法功能描述:根据指定的hql语句和参数查询语句</h3> 82 * </p> 83 * @param hql 需要执行的查询的HQL语句 84 * @param params 执行的查询的HQL语句所需的参数,如果没有填写null 85 * @return 返回查询数据的集合,如果出现异常返回null 86 * @procedure 执行过程 87 * @see IBaseDao 88 */ 89 List<T> find(String hql,Map<String, Object> params); 90 91 /** 92 * 93 * <p> 94 * <h3>方法功能描述:根据指定的HQL语句和参数以及分页所需的数据执行查询</h3> 95 * </p> 96 * @param hql 需要执行的查询的HQL语句 97 * @param pageIndex 需要查询的页数 98 * @param pageSize 每页显示的数据条数 99 * @param params 执行的查询的HQL语句所需的参数,如果没有填写null 100 * @return 返回分页查询的结果,是一个Map对象,该对象包含<blockquote> 101 * <b>data</b>:查询结果的数据集合是一个List对象<br> 102 * <b>pageIndex</b>:当前查询的页数<br> 103 * <b>pageSize</b>:每页显示的数据条数<br> 104 * <b>total</b>:数据的总条数<br> 105 * <b>pageTotal</b>:数据的总页数<br> 106 * <b>hasPrev</b>:是否有上一条数据<br> 107 * <b>hasNext</b>:是否有下一条数据<br> 108 * </blockquote> 109 * @procedure 执行过程 110 * @see IBaseDao 111 */ 112 Map<String, Object> find(String hql, int pageIndex, int pageSize, Map<String, Object> params); 113 114 /** 115 * 116 * <p> 117 * <h3>方法功能描述:分页查询数据</h3> 118 * </p> 119 * @param pageIndex 需要查询的页数 120 * @param pageSize 每页显示的数据条数 121 * @return 返回分页查询的结果,是一个Map对象,该对象包含<blockquote> 122 * <b>data</b>:查询结果的数据集合是一个List对象<br> 123 * <b>pageIndex</b>:当前查询的页数<br> 124 * <b>pageSize</b>:每页显示的数据条数<br> 125 * <b>total</b>:数据的总条数<br> 126 * <b>pageTotal</b>:数据的总页数<br> 127 * <b>hasPrev</b>:是否有上一条数据<br> 128 * <b>hasNext</b>:是否有下一条数据<br> 129 * </blockquote> 130 * @procedure 执行过程 131 * @see IBaseDao 132 */ 133 Map<String, Object> find(int pageIndex, int pageSize); 134 135 /** 136 * 137 * <p> 138 * <h3>方法功能描述:根据DetachedCriteria 对象设置的条件查询数据,该功能不具备分页操作</h3> 139 * </p> 140 * @param detachedCriteria 需要设置的对象查询条件 141 * @return 返回查询数据的集合,如果出现异常返回null 142 * @procedure 执行过程 143 * @see IBaseDao 144 */ 145 List<T> find(DetachedCriteria detachedCriteria); 146 147 148 /** 149 * 150 * <p> 151 * <h3>方法功能描述:根据DetachedCriteria 对象设置的条件进行分页查询</h3> 152 * </p> 153 * @param detachedCriteria 需要设置的对象查询条件 154 * @param pageIndex 需要查询的页数 155 * @param pageSize 每页显示的数据条数 156 * @return 返回分页查询的结果,是一个Map对象,该对象包含<blockquote> 157 * <b>data</b>:查询结果的数据集合是一个List对象<br> 158 * <b>pageIndex</b>:当前查询的页数<br> 159 * <b>pageSize</b>:每页显示的数据条数<br> 160 * <b>total</b>:数据的总条数<br> 161 * <b>pageTotal</b>:数据的总页数<br> 162 * <b>hasPrev</b>:是否有上一条数据<br> 163 * <b>hasNext</b>:是否有下一条数据<br> 164 * </blockquote> 165 * @procedure 拼接HQL语句 166 * @see IBaseDao 167 */ 168 Map<String, Object> find(DetachedCriteria detachedCriteria, int pageIndex, int pageSize); 169 170 }
b.IBaseDao接口实现类 -- BaseDa
1 package com.Elastic.StrutsDemo2.ivy.dao; 2 3 import java.io.Serializable; 4 import java.lang.reflect.ParameterizedType; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 import org.hibernate.Criteria; 9 import org.hibernate.Query; 10 import org.hibernate.Session; 11 import org.hibernate.criterion.DetachedCriteria; 12 import org.hibernate.criterion.Projections; 13 import com.Elastic.StrutsDemo2.ivy.util.HibernateUtil; 14 //忽略警告 15 @SuppressWarnings({ "rawtypes", "unchecked" }) 16 public class BaseDao<T> implements IBaseDao<T>{ 17 18 private Class entityClass; 19 20 public BaseDao(){ 21 entityClass = this.getEntityClass(); 22 } 23 24 /** 25 * 26 * <p> 27 * <h3>方法功能描述:根据反射得到当前泛型参数(实体类)的数据类型</h3> 28 * </p> 29 * @return 30 * @procedure 执行过程 31 * @see BaseDao 32 */ 33 private Class getEntityClass(){ 34 try { 35 ParameterizedType paramsType = (ParameterizedType)this.getClass().getGenericSuperclass();//只能通过父类得到 36 return (Class)paramsType.getActualTypeArguments()[0]; 37 } catch (Exception e) { 38 e.printStackTrace(); 39 } 40 return null; 41 } 42 43 @Override 44 public Session getSession() { 45 return HibernateUtil.getSession(); 46 } 47 48 private Map<String, Object> createPageData(List<T> data, int pageIndex, int pageSize, int total) { 49 Map<String, Object> map = new HashMap<String, Object>(); 50 51 //获得总页数 52 int pageTotal = (int)Math.ceil((double)total / pageSize); 53 map.put("data", data); 54 map.put("pageIndex", pageIndex); 55 map.put("pageSize", pageSize); 56 map.put("total", total); 57 map.put("pageTotal", pageTotal); 58 59 //boolean,若没赋值,默认true 60 map.put("hasPrev", pageIndex > 1); 61 map.put("hasNext", pageIndex < pageTotal); 62 return map; 63 } 64 65 /* (non-Javadoc) 66 * @see com.Elastic.StrutsDemo2.ivy.dao.IBaseDao#save(java.lang.Object) 67 */ 68 @Override 69 public void save(T record) { 70 this.getSession().save(record); 71 72 } 73 74 /* (non-Javadoc) 75 * @see com.Elastic.StrutsDemo2.ivy.dao.IBaseDao#delete(java.io.Serializable) 76 */ 77 @Override 78 public void delete(Serializable id) { 79 this.getSession().delete(this.findById(id)); 80 81 } 82 83 /* (non-Javadoc) 84 * @see com.Elastic.StrutsDemo2.ivy.dao.IBaseDao#delete(java.lang.Object) 85 */ 86 @Override 87 public void delete(T record) { 88 this.getSession().delete(record); 89 90 } 91 92 /* (non-Javadoc) 93 * @see com.Elastic.StrutsDemo2.ivy.dao.IBaseDao#update(java.lang.Object) 94 */ 95 @Override 96 public void update(T record) { 97 this.getSession().update(record); 98 99 } 100 101 /* (non-Javadoc) 102 * @see com.Elastic.StrutsDemo2.ivy.dao.IBaseDao#findById(java.io.Serializable) 103 */ 104 @Override 105 public T findById(Serializable id) { 106 return (T) this.getSession().get(entityClass, id); 107 } 108 109 /* (non-Javadoc) 110 * @see com.Elastic.StrutsDemo2.ivy.dao.IBaseDao#find(java.lang.String, java.util.Map) 111 */ 112 @Override 113 public List<T> find(String hql, Map<String, Object> params) { 114 Query query = this.getSession().createQuery(hql); 115 query.setProperties(params); 116 return query.list(); 117 } 118 119 /* (non-Javadoc) 120 * @see com.Elastic.StrutsDemo2.ivy.dao.IBaseDao#find(java.lang.String, int, int, java.util.Map) 121 */ 122 @Override 123 public Map<String, Object> find(String hql, int pageIndex, int pageSize, Map<String, Object> params) { 124 String hqlCount; 125 //通过给定的HQL语句查询全部条数 126 int i = hql.indexOf("from"); 127 if (i == 0) { 128 hqlCount = "select count(*) " + hql; 129 } else { 130 hqlCount = "select count(*) " + hql.substring(i); 131 } 132 133 Query query = this.getSession().createQuery(hql); 134 query.setProperties(params); 135 136 //index从0开始 137 query.setFirstResult((pageIndex - 1) * pageSize); 138 query.setMaxResults(pageSize); 139 140 int total = Integer.parseInt(this.getSession().createQuery(hqlCount).uniqueResult().toString()); 141 return this.createPageData(query.list(), pageIndex, pageSize, total); 142 } 143 144 /* (non-Javadoc) 145 * @see com.Elastic.StrutsDemo2.ivy.dao.IBaseDao#find(int, int) 146 */ 147 @Override 148 public Map<String, Object> find(int pageIndex, int pageSize) { 149 Criteria criteria = this.getSession().createCriteria(entityClass); 150 151 //查询总数 152 criteria.setProjection(Projections.rowCount()); 153 int total = Integer.parseInt(criteria.uniqueResult().toString()); 154 criteria.setProjection(null); 155 156 criteria.setFirstResult((pageIndex - 1) * pageSize); 157 criteria.setMaxResults(pageSize); 158 return this.createPageData(criteria.list(), pageIndex, pageSize, total); 159 } 160 161 /* (non-Javadoc) 162 * @see com.Elastic.StrutsDemo2.ivy.dao.IBaseDao#find(org.hibernate.criterion.DetachedCriteria) 163 */ 164 public List<T> find(DetachedCriteria detachedCriteria) { 165 return detachedCriteria.getExecutableCriteria(getSession()).list(); 166 } 167 168 /* (non-Javadoc) 169 * @see com.Elastic.StrutsDemo2.ivy.dao.IBaseDao#find(org.hibernate.criterion.DetachedCriteria, int, int) 170 */ 171 @Override 172 public Map<String, Object> find(DetachedCriteria detachedCriteria, int pageIndex, int pageSize) { 173 //查询总数 174 int total = Integer.parseInt(detachedCriteria.getExecutableCriteria(getSession()) 175 .setProjection(Projections.rowCount()) 176 .uniqueResult().toString()); 177 178 //分页查询 179 Criteria criteria = detachedCriteria.getExecutableCriteria(getSession()); 180 criteria.setFirstResult((pageIndex - 1) * pageSize); 181 criteria.setMaxResults(pageSize); 182 return this.createPageData(criteria.list(), pageIndex, pageSize, total); 183 } 184 }
c.UserDao接口
1 package com.Elastic.StrutsDemo2.ivy.dao; 2 import java.io.Serializable; 3 import com.Elastic.StrutsDemo2.ivy.entity.User; 4 public interface UserDao extends IBaseDao<User>{ 5 }
6.dao.impl包
a.UserDaoImpl
1 package com.Elastic.StrutsDemo2.ivy.dao.impl; 2 3 import java.io.Serializable; 4 import java.sql.Connection; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 import com.Elastic.StrutsDemo2.ivy.dao.BaseDao; 10 import com.Elastic.StrutsDemo2.ivy.dao.UserDao; 11 import com.Elastic.StrutsDemo2.ivy.entity.User; 12 public class UserDaoImpl extends BaseDao<User> implements UserDao{ 13 14 }
7.service包
a1.UserService接口
1 package com.Elastic.StrutsDemo2.ivy.service; 2 3 import com.Elastic.StrutsDemo2.ivy.entity.User; 4 public interface UserService { 5 6 // 名字和业务逻辑一样 7 User login(String userName, String passWord); 8 9 boolean registe(User user); 10 }
8.service.impl包
a1.UserServiceImpl
1 package com.Elastic.StrutsDemo2.ivy.service.impl; 2 3 import com.Elastic.StrutsDemo2.ivy.dao.UserDao; 4 import com.Elastic.StrutsDemo2.ivy.dao.impl.UserDaoImpl; 5 import com.Elastic.StrutsDemo2.ivy.entity.User; 6 import com.Elastic.StrutsDemo2.ivy.service.UserService; 7 import com.Elastic.StrutsDemo2.ivy.util.HibernateUtil; 8 public class UserServiceImpl implements UserService { 9 private UserDao userDao = new UserDaoImpl(); 10 11 /* (non-Javadoc) 12 * @see com.Elastic.SystemDemo.ivy.service.UserService#login(java.lang.String, java.lang.String) 13 */ 14 @Override 15 public User login(String userName, String passWord) { 16 17 User user = userDao.findById(userName); 18 if (null != user && user.getPassWord().equals(passWord)) { 19 // 密码设为null,不记录passWord 20 user.setPassWord(null); 21 return user; 22 } 23 return null; 24 } 25 26 /* (non-Javadoc) 27 * @see com.Elastic.StrutsDemo2.ivy.service.UserService#registe(com.Elastic.StrutsDemo2.ivy.entity.User) 28 */ 29 @Override 30 public boolean registe(User user) { 31 try { 32 userDao.save(user); 33 34 //每个增、删、改操作都要开启事务,除查之外 35 HibernateUtil.getSession().getTransaction().commit(); 36 } catch (Exception e) { 37 return false; 38 } 39 return true; 40 } 41 42 }
9.action包
a1.UserAction
1 package com.Elastic.StrutsDemo2.ivy.action; 2 3 import java.util.Map; 4 5 import com.Elastic.StrutsDemo2.ivy.entity.User; 6 import com.Elastic.StrutsDemo2.ivy.service.UserService; 7 import com.Elastic.StrutsDemo2.ivy.service.impl.UserServiceImpl; 8 import com.opensymphony.xwork2.ActionContext; 9 import com.opensymphony.xwork2.ActionSupport; 10 public class UserAction extends ActionSupport{ 11 private User user; 12 13 public User getUser() { 14 return user; 15 } 16 17 public void setUser(User user) { 18 this.user = user; 19 } 20 21 /* (non-Javadoc) 22 * @see com.opensymphony.xwork2.Action#execute() 23 */ 24 public String login(){ 25 System.out.println("登录"); 26 UserService userService = new UserServiceImpl(); 27 28 //获取到注册的user信息 29 //★★★★★注意:重定向 ,上下文不一样(请求一次和响应一次,之后服务器不管。下次再请求一次和响应一次,之前的请求响应就丢失了)★★★★★ 30 //所以要得到session的id 31 User regUser = (User) ActionContext.getContext().getSession().get("regUser"); 32 System.out.println(regUser); 33 user = regUser == null ? user : regUser; 34 ActionContext.getContext().put("regUser", null); 35 System.out.println(user.getUserName()); 36 37 //获取登录的user信息 38 User loginUser = userService.login(user.getUserName(), user.getPassWord()); 39 if (null != loginUser) { 40 return SUCCESS; 41 } 42 return ERROR; 43 } 44 45 public String registe(){ 46 UserService userService = new UserServiceImpl(); 47 if (userService.registe(user)) { 48 System.out.println(SUCCESS); 49 //要得到session的id,以便重定向成功 50 ActionContext.getContext().getSession().put("regUser", user); 51 return SUCCESS; 52 } 53 return ERROR; 54 } 55 }
a2.动态结果 -- UserAction
1 package com.Elastic.StrutsDemo2.ivy.action; 2 3 import java.util.Map; 4 5 import com.Elastic.StrutsDemo2.ivy.entity.User; 6 import com.Elastic.StrutsDemo2.ivy.service.UserService; 7 import com.Elastic.StrutsDemo2.ivy.service.impl.UserServiceImpl; 8 import com.opensymphony.xwork2.ActionContext; 9 import com.opensymphony.xwork2.ActionSupport; 10 public class UserAction extends ActionSupport{ 11 private User user; 12 13 //配置时不知道执行后的结果是哪一个,运行时才知道哪个结果作为视图显示给用户 14 private String nextUrl; 15 16 public User getUser() { 17 return user; 18 } 19 20 public void setUser(User user) { 21 this.user = user; 22 } 23 24 public String getNextUrl() { 25 return nextUrl; 26 } 27 28 public void setNextUrl(String nextUrl) { 29 this.nextUrl = nextUrl; 30 } 31 32 /* (non-Javadoc) 33 * @see com.opensymphony.xwork2.Action#execute() 34 */ 35 public String login(){ 36 System.out.println("登录"); 37 UserService userService = new UserServiceImpl(); 38 39 //获取到注册的user信息 40 //★★★★★注意:重定向 ,上下文不一样(请求一次和响应一次,之后服务器不管。下次再请求一次和响应一次,之前的请求响应就丢失了)★★★★★ 41 //所以要得到session的id 42 User regUser = (User) ActionContext.getContext().getSession().get("regUser"); 43 System.out.println(regUser); 44 user = regUser == null ? user : regUser; 45 ActionContext.getContext().put("regUser", null); 46 System.out.println(user.getUserName()); 47 48 //获取登录的user信息 49 User loginUser = userService.login(user.getUserName(), user.getPassWord()); 50 if (null != loginUser) { 51 nextUrl = "/index.jsp"; 52 return SUCCESS; 53 } 54 return ERROR; 55 } 56 57 public String registe(){ 58 UserService userService = new UserServiceImpl(); 59 if (userService.registe(user)) { 60 System.out.println(SUCCESS); 61 //要得到session的id,以便重定向成功 62 ActionContext.getContext().getSession().put("regUser", user); 63 return SUCCESS; 64 } 65 return ERROR; 66 } 67 }
10.Struts配置文件
a1.动态方法调用 -- struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <!-- 设置编码,默认与操作系统有关 --> 8 <constant name="struts.i18n.encoding" value="utf-8" ></constant> 9 10 <!-- 设置动态方法调用 --> 11 <!-- 设置动态方法调用,不是false??? --> 12 <constant name="struts.enable.DynamicMethodInvocation" value="true" ></constant> 13 14 <package name="default" namespace="/user" extends="struts-default"><!-- 包继承包 --> 15 <!-- action中name的值与表单的action属性值对应 --> 16 <!-- 请求地址:login class:action(servlet)地址--> 17 <action name="login" class="com.Elastic.StrutsDemo2.ivy.action.UserAction" method="login"> 18 <!-- result中name的与Action中的execute()返回的值一样 --> 19 <result name="success">/index.jsp</result> 20 <!-- 登录失败 --> 21 <result name="error">/error.jsp</result> 22 </action> 23 24 <action name="reg" class="com.Elastic.StrutsDemo2.ivy.action.UserAction" method="registe"> 25 <!-- result中name的与Action中的execute()返回的值一样 --> 26 <result name="success" type="redirectAction">login</result> 27 <!-- 登录失败 --> 28 <result name="error" type="redirect">/error.jsp</result> 29 </action> 30 </package> 31 </struts>
a2.动态调用:通配符(*) -- struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <!-- 设置编码,默认与操作系统有关 --> 8 <constant name="struts.i18n.encoding" value="utf-8" ></constant> 9 10 <package name="default" namespace="/user" extends="struts-default"><!-- 包继承包 --> 11 12 <!-- action中name的值与表单的action属性值对应 --> 13 <!-- 请求地址:login class:action(servlet)地址 --> 14 <action name="login" class="com.Elastic.StrutsDemo2.ivy.action.UserAction" method="login"> 15 <!-- result中name的与Action中的execute()返回的值一样 --> 16 <result name="success">/index.jsp</result> 17 <!-- 登录失败 --> 18 <result name="error">/error.jsp</result> 19 </action> 20 21 <action name="reg" class="com.Elastic.StrutsDemo2.ivy.action.UserAction" method="registe"> 22 <!-- result中name的与Action中的execute()返回的值一样 --> 23 <result name="success" type="redirectAction">login</result> 24 <!-- 登录失败 --> 25 <result name="error" type="redirect">/error.jsp</result> 26 </action> 27 28 <action name="*User" class="com.Elastic.StrutsDemo2.ivy.action.UserAction" method="{1}"> 29 <result name="success">{1}_success.jsp</result> 30 <result name="error">{1}_error.jsp</result> 31 </action> 32 33 </package> 34 </struts>
a3.配置默认Action -- struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <!-- 设置编码,默认与操作系统有关 --> 8 <constant name="struts.i18n.encoding" value="utf-8" ></constant> 9 10 <package name="default" namespace="/user" extends="struts-default"><!-- 包继承包 --> 11 12 <!-- 配置默认的action --> 13 <default-action-ref name="errorAction"></default-action-ref> 14 <action name="errorAction"> 15 <result name="">/error.jsp</result> 16 </action> 17 18 <!-- action中name的值与表单的action属性值对应 --> 19 <!-- 请求地址:login class:action(servlet)地址 --> 20 <action name="login" class="com.Elastic.StrutsDemo2.ivy.action.UserAction" method="login"> 21 <!-- result中name的与Action中的execute()返回的值一样 --> 22 <result name="success">/index.jsp</result> 23 <!-- 登录失败 --> 24 <result name="error">/error.jsp</result> 25 </action> 26 27 </package> 28 </struts>
a4.转发类型 -- struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <!-- 设置编码,默认与操作系统有关 --> 8 <constant name="struts.i18n.encoding" value="utf-8" ></constant> 9 10 <!-- 设置动态方法调用 --> 11 <constant name="struts.enable.DynamicMethodInvocation" value="true" ></constant> 12 13 <package name="default" namespace="/user" extends="struts-default"><!-- 包继承包 --> 14 <!-- action中name的值与表单的action属性值对应 --> 15 <!-- 请求地址:login class:action(servlet)地址 --> 16 <action name="login" class="com.Elastic.StrutsDemo2.ivy.action.UserAction" method="login"> 17 <!-- result中name的与Action中的execute()返回的值一样 --> 18 <result name="success">/index.jsp</result> 19 <!-- 登录失败 --> 20 <result name="error">/error.jsp</result> 21 </action> 22 23 <action name="reg" class="com.Elastic.StrutsDemo2.ivy.action.UserAction" method="registe"> 24 <!-- 登陆成功:重定向到 action(redirectAction) --> 25 <result name="success" type="redirectAction">login</result> 26 27 <!-- 验证失败,转发到登陆页面(dispatcher) --> 28 <result name="input" type="dispatcher">/login.jsp</result> 29 30 <!-- 登录失败:重定向到错误页面(redirect) --> 31 <result name="error" type="redirect">/error.jsp</result> 32 </action> 33 34 </package> 35 </struts>
a5.动态结果 -- struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <!-- 设置编码,默认与操作系统有关 --> 8 <constant name="struts.i18n.encoding" value="utf-8" ></constant> 9 10 <!-- 设置动态方法调用 --> 11 <constant name="struts.enable.DynamicMethodInvocation" value="true" ></constant> 12 13 <package name="default" namespace="/user" extends="struts-default"><!-- 包继承包 --> 14 15 <default-action-ref name="errorAction"></default-action-ref> 16 <!-- 配置默认的action --> 17 <action name="errorAction"> 18 <result name="">/error.jsp</result> 19 </action> 20 21 <!-- action中name的值与表单的action属性值对应 --> 22 <!-- 请求地址:login class:action(servlet)地址 --> 23 <action name="login" class="com.Elastic.StrutsDemo2.ivy.action.UserAction" method="login"> 24 <!-- result中name的与Action中的execute()返回的值一样 --> 25 <result name="success">${nextUrl}</result> 26 <!-- 登录失败 --> 27 <result name="error">/error.jsp</result> 28 </action> 29 30 <action name="reg" class="com.Elastic.StrutsDemo2.ivy.action.UserAction" method="registe"> 31 <!-- 登陆成功:重定向到 action(redirectAction) --> 32 <result name="success" type="redirectAction">login</result> 33 34 <!-- 验证失败,转发到登陆页面(dispatcher) --> 35 <result name="input" type="dispatcher">/login.jsp</result> 36 37 <!-- 登录失败:重定向到错误页面(redirect) --> 38 <result name="error" type="redirect">/error.jsp</result> 39 </action> 40 41 </package> 42 </struts>
11.jsp包
a.success.jsp
1 <%-- 引入JSP页面PAGE指令 --%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <%-- 引入JSTL标签指令 --%> 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 6 <%@ taglib uri="/struts-tags" prefix="s" %> 7 <!DOCTYPE html> 8 <html language="zh-CN"> 9 <head> 10 <meta charset="utf-8"> 11 <!-- 设置浏览器渲染的引擎 --> 12 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 13 <!-- 设置支持移动设备 --> 14 <meta name="viewport" content="width=device-width, initial-scale=1"> 15 <title>网页标题</title> 16 <!-- 引用bootstrap样式 --> 17 <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.min.css"> 18 </head> 19 <body> 20 <%-- <s:debug></s:debug> --%> 21 <div class="container-fluid"> 22 <%-- <h1>${sessionScope.loginUser }登录成功</h1> --%> 23 <div class="panel panel-primary"> 24 <div class="panel-heading">提示信息</div> 25 <div class="panel-body text-center"> 26 登录成功!<span id="time">10</span>秒后页面自动跳转到首页<br> 27 如果没有跳转,请点击<a href="index.jsp">此处</a> 28 </div> 29 </div> 30 </div> 31 32 <!-- 引用外部JS文件 --> 33 <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-2.2.4.js"></script> 34 <script type="text/javascript" src="<%=request.getContextPath() %>/js/bootstrap.min.js"></script> 35 <script type="text/javascript"> 36 var time = 10; 37 function returnPage() { 38 time --; 39 if (time <= 0) { 40 window.location = 'index.jsp'; 41 } 42 $('#time').html(time); 43 } 44 45 $(function() { 46 window.setInterval(returnPage, 1000); 47 }); 48 49 </script> 50 </body> 51 </html>
b.error.jsp
1 <%-- 引入JSP页面PAGE指令 --%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <%-- 引入JSTL标签指令 --%> 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 6 <!DOCTYPE html> 7 <html language="zh-CN"> 8 <head> 9 <meta charset="utf-8"> 10 <!-- 设置浏览器渲染的引擎 --> 11 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 12 <!-- 设置支持移动设备 --> 13 <meta name="viewport" content="width=device-width, initial-scale=1"> 14 <title>错误页面</title> 15 <!-- 引用bootstrap样式 --> 16 <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.min.css"> 17 </head> 18 <body> 19 <div class="container-fluid"> 20 21 </div> 22 23 <!-- 引用外部JS文件 --> 24 <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-2.2.4.js"></script> 25 <script type="text/javascript" src="<%=request.getContextPath() %>/js/bootstrap.min.js"></script> 26 27 </body> 28 </html>
c.index.jsp
1 <%-- 引入JSP页面PAGE指令 --%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <%-- 引入JSTL标签指令 --%> 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 6 <!DOCTYPE html> 7 <html language="zh-CN"> 8 <head> 9 <meta charset="utf-8"> 10 <!-- 设置浏览器渲染的引擎 --> 11 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 12 <!-- 设置支持移动设备 --> 13 <meta name="viewport" content="width=device-width, initial-scale=1"> 14 <title>首页</title> 15 <!-- 引用bootstrap样式 --> 16 <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.min.css"> 17 </head> 18 <body> 19 20 <!-- 引用外部JS文件 --> 21 <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-2.2.4.js"></script> 22 <script type="text/javascript" src="<%=request.getContextPath() %>/js/bootstrap.min.js"></script> 23 </body> 24 </html>
d.login.jsp
1 <%-- 引入JSP页面PAGE指令 --%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <%-- 引入JSTL标签指令 --%> 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 6 <%@ taglib uri="/struts-tags" prefix="s"%> 7 <!DOCTYPE html> 8 <html language="zh-CN"> 9 <head> 10 <meta charset="utf-8"> 11 <!-- 设置浏览器渲染的引擎 --> 12 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 13 <!-- 设置支持移动设备 --> 14 <meta name="viewport" content="width=device-width, initial-scale=1"> 15 <title>网页标题</title> 16 <!-- 引用bootstrap样式 --> 17 <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.min.css"> 18 </head> 19 <body> 20 <%-- <s:debug></s:debug> --%> 21 <div class="container-fluid"> 22 <div class="panel panel-default center-block" style="width: 500px;"> 23 <div class="panel-heading">登录</div> 24 <div class="panel-body"> 25 <form action="user/login" method="post" class="form-horizontal"> 26 <div class="form-group"> 27 <label class="col-md-3 control-label" for="userName">用户名:</label> 28 <div class="col-md-6"> 29 <input class="form-control" id="userName" name="user.userName" 30 type="text" autocomplete="off" /> 31 </div> 32 <div class="col-md-3"> 33 <%-- <s:fielderror fieldName="userName" theme="simple"></s:fielderror> --%> 34 ${fieldErrors.userName[0] } 35 </div> 36 </div> 37 <div class="form-group"> 38 <label class="col-md-3 control-label" for="passWord">密码:</label> 39 <div class="col-md-6"> 40 <input class="form-control" id="passWord" name="user.passWord" 41 type="password" /> 42 </div> 43 <div class="col-md-3"> 44 45 </div> 46 </div> 47 <div class="form-group"> 48 <input class="btn btn-primary btn-block" type="submit" value="登录" /> 49 </div> 50 </form> 51 </div> 52 </div> 53 </div> 54 55 <!-- 引用外部JS文件 --> 56 <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-2.2.4.js"></script> 57 <script type="text/javascript" src="<%=request.getContextPath() %>/js/bootstrap.min.js"></script> 58 59 </body> 60 </html>
e.reg.jsp
1 <%-- 引入JSP页面PAGE指令 --%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <%-- 引入JSTL标签指令 --%> 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 6 <%@ taglib uri="/struts-tags" prefix="s"%> 7 <!DOCTYPE html> 8 <html language="zh-CN"> 9 <head> 10 <meta charset="utf-8"> 11 <!-- 设置浏览器渲染的引擎 --> 12 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 13 <!-- 设置支持移动设备 --> 14 <meta name="viewport" content="width=device-width, initial-scale=1"> 15 <title>网页标题</title> 16 <!-- 引用bootstrap样式 --> 17 <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.min.css"> 18 </head> 19 <body> 20 <%-- <s:debug></s:debug> --%> 21 <div class="container-fluid"> 22 <div class="panel panel-default center-block" style="width: 500px;"> 23 <div class="panel-heading">注册</div> 24 <div class="panel-body"> 25 <form action="user/reg!registe" method="post" class="form-horizontal"> 26 <div class="form-group"> 27 <label class="col-md-3 control-label" for="userName">用户名:</label> 28 <div class="col-md-6"> 29 <input class="form-control" id="userName" name="user.userName" 30 type="text" autocomplete="off" /> 31 </div> 32 <div class="col-md-3"> 33 <%-- <s:fielderror fieldName="userName" theme="simple"></s:fielderror> --%> 34 ${fieldErrors.userName[0] } 35 </div> 36 </div> 37 <div class="form-group"> 38 <label class="col-md-3 control-label" for="passWord">密码:</label> 39 <div class="col-md-6"> 40 <input class="form-control" id="passWord" name="user.passWord" 41 type="password" /> 42 </div> 43 <div class="col-md-3"> 44 45 </div> 46 </div> 47 <div class="form-group"> 48 <input class="btn btn-primary btn-block" type="submit" value="注册" /> 49 </div> 50 </form> 51 </div> 52 </div> 53 </div> 54 55 <!-- 引用外部JS文件 --> 56 <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-2.2.4.js"></script> 57 <script type="text/javascript" src="<%=request.getContextPath() %>/js/bootstrap.min.js"></script> 58 59 </body> 60 </html>
e1.动态方法调用 -- reg.jsp
1 <%-- 引入JSP页面PAGE指令 --%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <%-- 引入JSTL标签指令 --%> 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 6 <%@ taglib uri="/struts-tags" prefix="s"%> 7 <!DOCTYPE html> 8 <html language="zh-CN"> 9 <head> 10 <meta charset="utf-8"> 11 <!-- 设置浏览器渲染的引擎 --> 12 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 13 <!-- 设置支持移动设备 --> 14 <meta name="viewport" content="width=device-width, initial-scale=1"> 15 <title>网页标题</title> 16 <!-- 引用bootstrap样式 --> 17 <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.min.css"> 18 </head> 19 <body> 20 <%-- <s:debug></s:debug> --%> 21 <div class="container-fluid"> 22 <div class="panel panel-default center-block" style="width: 500px;"> 23 <div class="panel-heading">注册</div> 24 <div class="panel-body"> 25 <!--严格标准:user/reg!registe.action--> 26 <form action="user/reg!registe" method="post" class="form-horizontal"> 27 <div class="form-group"> 28 <label class="col-md-3 control-label" for="userName">用户名:</label> 29 <div class="col-md-6"> 30 <input class="form-control" id="userName" name="user.userName" 31 type="text" autocomplete="off" /> 32 </div> 33 <div class="col-md-3"> 34 <%-- <s:fielderror fieldName="userName" theme="simple"></s:fielderror> --%> 35 ${fieldErrors.userName[0] } 36 </div> 37 </div> 38 <div class="form-group"> 39 <label class="col-md-3 control-label" for="passWord">密码:</label> 40 <div class="col-md-6"> 41 <input class="form-control" id="passWord" name="user.passWord" 42 type="password" /> 43 </div> 44 <div class="col-md-3"> 45 46 </div> 47 </div> 48 <div class="form-group"> 49 <input class="btn btn-primary btn-block" type="submit" value="注册" /> 50 </div> 51 </form> 52 </div> 53 </div> 54 </div> 55 56 <!-- 引用外部JS文件 --> 57 <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-2.2.4.js"></script> 58 <script type="text/javascript" src="<%=request.getContextPath() %>/js/bootstrap.min.js"></script> 59 60 </body> 61 </html>
e2.动态调用:通配符(*) -- reg.jsp
1 <%-- 引入JSP页面PAGE指令 --%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <%-- 引入JSTL标签指令 --%> 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 6 <%@ taglib uri="/struts-tags" prefix="s"%> 7 <!DOCTYPE html> 8 <html language="zh-CN"> 9 <head> 10 <meta charset="utf-8"> 11 <!-- 设置浏览器渲染的引擎 --> 12 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 13 <!-- 设置支持移动设备 --> 14 <meta name="viewport" content="width=device-width, initial-scale=1"> 15 <title>网页标题</title> 16 <!-- 引用bootstrap样式 --> 17 <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.min.css"> 18 </head> 19 <body> 20 <%-- <s:debug></s:debug> --%> 21 <div class="container-fluid"> 22 <div class="panel panel-default center-block" style="width: 500px;"> 23 <div class="panel-heading">注册</div> 24 <div class="panel-body"> 25 <form action="user/reg" method="post" class="form-horizontal"> 26 <div class="form-group"> 27 <label class="col-md-3 control-label" for="userName">用户名:</label> 28 <div class="col-md-6"> 29 <input class="form-control" id="userName" name="user.userName" 30 type="text" autocomplete="off" /> 31 </div> 32 <div class="col-md-3"> 33 <%-- <s:fielderror fieldName="userName" theme="simple"></s:fielderror> --%> 34 ${fieldErrors.userName[0] } 35 </div> 36 </div> 37 <div class="form-group"> 38 <label class="col-md-3 control-label" for="passWord">密码:</label> 39 <div class="col-md-6"> 40 <input class="form-control" id="passWord" name="user.passWord" 41 type="password" /> 42 </div> 43 <div class="col-md-3"> 44 45 </div> 46 </div> 47 <div class="form-group"> 48 <input class="btn btn-primary btn-block" type="submit" value="注册" /> 49 </div> 50 </form> 51 </div> 52 </div> 53 </div> 54 55 <!-- 引用外部JS文件 --> 56 <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-2.2.4.js"></script> 57 <script type="text/javascript" src="<%=request.getContextPath() %>/js/bootstrap.min.js"></script> 58 59 </body> 60 </html>
12.web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <display-name>StrutsDemo2_ivy</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- 先配置OpenSeesionInView --> 14 <filter> 15 <filter-name>OpenSeesionInView</filter-name> 16 <filter-class>com.Elastic.StrutsDemo2.ivy.filter.OpenSessionInViewFilter</filter-class> 17 </filter> 18 <filter-mapping> 19 <filter-name>OpenSeesionInView</filter-name> 20 <url-pattern>/*</url-pattern> 21 </filter-mapping> 22 23 <!-- 配置Struts2的核心过滤器{Controller} --> 24 <filter> 25 <filter-name>Struts2</filter-name> 26 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 27 </filter> 28 <filter-mapping> 29 <filter-name>Struts2</filter-name> 30 <url-pattern>/*</url-pattern> 31 </filter-mapping> 32 33 </web-app>
注意:
1.查询不要提交事务
2.增删改都需要提交事务
3.重定向 上下文不一样(请求一次和响应一次,之后服务器不管。下次再请求一次和响应一次,之前的请求响应就丢失了)