1 前步骤:Struts2.1+Spring3.0+Hibernate3.3
http://xly3000.blog.163.com/blog/static/1324247201231163828803/
2 struts2实例和详细介绍(百度文库)
http://wenku.baidu.com/link?url=TmROnixn9KdoNx3VHj1sLzPtHMWYfoo8RkPvivHscJx98HEI2jbhGd6kMedqQOUDx6PfmvHg6qWxgGmg0ZWI-nt_oAR9CT8_ixrRIwz_PNO
3 问题1
Unable to load configuration. - action - file:/C:/Program%20Files/apache-tomcat-7.0.5/webapps/ch02/WEB-INF/classes/struts.xml:7:76
导入struts2-spring-plugin-2.1.8.1.jar 即可
4 然后出现
Line: 220 - com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1
web.xm添加
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
原因:http://javeye.iteye.com/blog/940122/
5 spring 配置
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306"></property>
<property name="username" value="*"></property>
<property name="password" value="*"></property>
………………………………
修改为:
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass"
value="com.mysql.jdbc.Driver">
</property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hcwl"></property>
<property name="user" value="*"></property>
<property name="password" value="*"></property>
<!--连接池中保留的最小连接数。 -->
<property name="minPoolSize">
<value>5</value>
</property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize">
<value>300</value>
</property>
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize">
<value>10</value>
</property>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime">
<value>60</value>
</property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement">
<value>5</value>
</property>
<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements">
<value>0</value>
</property>
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod">
<value>60</value>
</property>
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<property name="acquireRetryAttempts">
<value>30</value>
</property>
<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
<property name="breakAfterAcquireFailure">
<value>true</value>
</property>
<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
等方法来提升连接测试的性能。Default: false -->
<property name="testConnectionOnCheckout">
<value>false</value>
</property>
</bean>
*注意属性名变化
6 model接口说明
extends HibernateDaoSupport
this.getHibernateTemplate().find(hql)
7 spring 注入接口
<bean id="udao" class="com.hcwl.dao.imp.HcwlUsersDaoImp">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="uservice" class="com.hcwl.service.HcwlUsersServiceImp">
<property name="hcwlUsersDao" ref="udao"></property>
</bean>
8 控制类说明
extends ActionSupport
或 implment Action
9 页面说明
<input type="text" name="hcwlUsers.userAccount" class="in" id="adminname"/>
10 中文乱码问题
(0)jsp页面
(a)
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
(b)
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
(1)Struts.xml :
添加 <constant name="struts.i18n.encoding" value="UTF-8"></constant>
(2)web.xml
添加
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>com.youname.util.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
其中com.youname.util.CharacterEncodingFilter
public class CharacterEncodingFilter implements Filter {
protected FilterConfig filterConfig = null;
protected String encoding = "";
public void destroy() {
filterConfig = null;
encoding = null;
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
if(encoding != null && !"".equals(encoding))
servletRequest.setCharacterEncoding(encoding);
filterChain.doFilter(servletRequest, servletResponse);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
System.out.println("----------"+this.encoding);
}
}
(3) tomcat 服务器 server.xml
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
(4)mysql 数据库
停掉mysql服务后,修改mysql中的配置文件my.ini,
在[mysql]标签中修改default-character-set=utf8
在[client]标签中添加default-character-set=utf8
在[mysqld]标签中修改default-character-set=utf8
在[mysqld]标签中添加character_set_server = utf8
#表名不区分大小写(此与编码无关)
lower_case_table_names = 1
添加标签[mysql.server]和[mysql_safe] 并分别在标签下添加
default-character-set=utf8
* 此处请随机应变 当前例子5.5版本 只修改了mysql 和mysqld 两处
mysql 查验
命令参考
net stop mysql
net start mysql
mysql -h.. -u.. -p.. (..分别为IP地址,用户名,密码)进入
use your_db_name;
show variables like 'character\_set\_%';
11 文本编辑器
SSH集成CKEditor和CKFinder
http://wenku.baidu.com/view/a4083a5f6f1aff00bed51e9b.html
* 中间出现了些小插曲
ckeditor 4.4.7
ckfinder 2.5
本来上传功能已OK,但后来改动别的地方不心影响到此处(再次证明阶段备份很重要),点击上传资料无反响,实际是有报错的,只不过那块儿空间太小了
错误如下:
HTTP Status 404 - /jsp_dir/ckfinder/core/connector/java/connector.java
type Status report
message /ckfinder/core/connector/java/connector.java
description The requested resource is not available.
Apache Tomcat/7.0.32
网上苦苦寻找解决方案没有一个可以对的上,但给了很多参考建议,
问题根源就是路径问题
我当时目录情况如下
ckfinder,ckeditor 都在根目下WebRoot/
jsp在WebRoot/jsp_dir/ 下。
如果入口的JSP同样在WebRoot 下的话,正常无问题
但放到WebRoot 下的子目录 便会出现上面的问题。
很奇怪之前是怎么成功的。严重怀疑 原因如下(因为懒所以不愿去翻源码也未回头再验证)
当然其它路径需要照上面的的教程修改。
后问题解决方法为:我把ckfinder,ckeditor也放到了那个目录下并做了相应修改(好吧~ 我确实偷懒了,不过我确实觉得在那里放着也挺合适的)
12 文字提交不上去 提交方式改post即可
13 登录验证拦截
两种方法 一种在web.xml中添加过滤器,另一种为使用struts2拦截器
使用第二种
<package ...>
<!-- 用户拦截器定义在该元素下 -->
<interceptors>
<!-- 定义了一个名为authority的拦截器 -->
<interceptor name="authenticationInterceptor" class="com.hcwl.util.filter.AuthInterceptor" />
<interceptor-stack name="defualtSecurityStackWithAuthentication">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authenticationInterceptor" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defualtSecurityStackWithAuthentication" />
…………
</package>
*注意package 的namespace
类如下:
public class AuthInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = -5114658085937727056L;
private String sessionKey="manager";
private String parmKey="withoutAuthentication";
private boolean excluded;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext ac=invocation.getInvocationContext();
Map<?, ?> session =ac.getSession();
String parm=(String) ac.getParameters().get(parmKey);
if(parm!=null){
excluded=parm.toUpperCase().equals("TRUE");
}
HcwlUsers user=(HcwlUsers)session.get(sessionKey);
if(excluded || user!=null){
return invocation.invoke();
}
ac.put("tip", "您还没有登录!");
//直接返回 login 的逻辑视图
return Action.LOGIN;
}
}