shangXueChang Mashibing shs 整合项目创建由零开始

环境:eclipse 3.6 + 

创建项目

一、修改JSP文件 文字样式

       Preferendce ->输入 jsp ->选中 jsp Files  -> 将encoding 设置为 chinese ,National Standard  ,每次创建的jsp文件,默认GB18030GB18030 GB18030

二、创建数据库   mysql -uroot  -p 

 

三、报错

The server does not support version 3.0 of the J2EE Web module specification

在项目根目录下有一个.settings的文件夹,该文件夹下有一个org.eclipse.wst.common.project.facet.core.xml文件,

<installed facet="jst.web" version="3.0"/>

改为

<installed facet="jst.web" version="2.5"/>

即可

四、报错  java.runningtime classNotFound    Class.forName("com.mysql.jdbc.Driver");
  将mysql-connetion-java -5.1.10.jar  jar包 放 Tomcat目录 lib目录下
五、修改项目 目录  preference 输入web  修改 

上面的异常的抛出主要有几个方面:1、最容易想到的,就是你的from是实体类而不是表名,这个应该大家都知道,注意大小写

2、你的hibernate配置文件中没有加入相应的映射文件<mapping resource=" " />

3、如果上面两方面都对了,还有错,基本上处于崩溃状态,哈哈....一般是上面两种原因,还是不行,那就是你hql语句有错了,好好检查一下,仔细点,不行就重新写一遍,因为我就是因为一个中文空格被折腾了两天啊,就是找不出哪儿出错了,怎么看怎么对,可是就是报错,哈哈...很崩溃的。

4、检查引入的包是否有错 以下正确引入包
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
 七、MVC 模式 (切换到编辑看)
 |客户端|   |     control层        |        |   view 层  |      |   model 层   |
-----  ---------   ------      --------       ----------    -------
|client|  |struct filter|  |action |   |view.jsp  view.html|    |UserManager.java |   | DB |
                                                                   |UserDao.java     | 
                                                                   |User.java        |

八、structs2 接收参数五种模式  前三种常用
  第一种 用action属性接收    以private String username;
  第二种 用demain model
  第三种 用DTO
  第四种 用modelDriver 
  第五种 用requst对象

九、spring 构建两种方法 Bean  
    1 在Bean.xml 配置 
    2 标记 @Compont 

十、整合structs 2 

 整合步骤:

1.       修改web.xml加入 struts的filter

2.       再加入spring的listener,这样的话,webapp一旦启动,spring容器就初始化了

3.       规划struts的action和jsp展现

4.       加入struts.xml

a)      修改配置,由structs-spring-plugin插件或 spring 容 器  替代struts产生Action对象

5.       修改action配置

a)      把类名改为bean对象的名称,这个时候就可以使用首字母小写了

   struts读常量查找文件顺序:

1.       struts-default.xml

2.       struts-plugin.xml

3.       struts.xml

4.       struts.properties

5.       web.xml


   第40讲 和 第49讲 分别介绍两种生成、管理 action bean 方法 
第一种方法(action头上什么都不用写anotation 标记)由structs2-spring-plugin 管理

 结合点:Struts2 通过自身的 spring -plugin 插件生成action bean ,默认@scope属性为prototype 。生成的action bean 与spring 生成的bean 分开 用两个容器管理,action bean 头上不用写anotation 标记,struct2-spring-plugin 插件默认情况下,会根据struct2.xml 配置文件的class路径,生成 一个 action bean ,名字 与struct2.xml 配置的action名字一致,如果再加上anotation 标记,例如: @compont("user "),则 spring 会为其又生产一个action bean ,一共有两个action bean ,一个由structs2 -spring -plugin 插件管理,另一个由spring  管理,但当action bean 需要用到的bean (非action Bean , 例如:Dao层 的 userDao bean 、service 层的userManager bean ),则由Spring 装载到action bean中.。


第二种方法  action头上加anotation 标记)由spring容器管理

在action 头上加 @compont("user") 和  @Scope(“prototype”)标记,prototype 表示每次请求都生成一新的action bean ,这个标记必须记得写,要想将Bean交给spring 管理,在structs2 配置文件中 ,将action 的class路去掉,改为 bean 的名字,这样spring 就会为structs2 装载action bean ;



十一、第44讲 报错:can not  convert type [ action ] to required type into bean 
  由于action的名字 user 与 请求一个user Bean名字一致,导致spring 将action user bean注入到user bean里面。  struct 使用手册说到使用spring—plugin 插件 ,  在 struct2.ObjectFactory.spring.autowrite 没有配置任何查找类型时(name、type、auto or construtor),默认情况下按 name 来查找 ;
解决方法:action 取名不能bean的名子相同


十二、第45讲 20分45秒  报错:could not initilize proxy - no session   这是一个经典错误
     原因 由于使用 hibernateTemplate 的 load方法,会先生成一个代理,当serviec层的load的方法执行完毕后,session 就结束,从而导致 在jsp页面执行<s:property value="user.username“> 这句时,发出的sql语句,而session 已经关闭。
解决方法:在web.xml添加拦载器,其作用在 jsp 页面执行<s:property value="user.username“>执行前拦截,待其执行完毕后,再进行拦截从而关闭session.
在加入拦截器注意,此拦载器必须加在 struct2 拦截器前面
<filter>
 <filter-name>openSessionInView</filter-name>
  <filterclass>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
  </filter-class>
  <init-param>
        <!--以下两句是为filter配置参数,因该filter需要一个sessionFactory-->
<param-name>sessionFactoryBeanName</param-name>
<param-value>sf</param-value>
  </init-param>
</filter>

<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
  
虽然上这个事务过滤器,但如没有在spring bean.xml配置事务拦截器(因为没有配置事务拦截器,默认只有“读”的权限,所以在注册用户时,向数据库添加数据报错),报错:Write operation are not allowed in read-only model

  <aop:config>
      <aop:pointcut id="bussinessService"
          expression="execution(public * com.bjsxt.registration.service.*.*(..))" />
      <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
  </aop:config>
  <tx:advice id="txAdvice" transaction-manager="txManager">
     <tx:attributes>
         <tx:method name="exists" read-only="true" />
         <tx:method name="add*" propagation="REQUIRED"/>
     </tx:attributes>
  </tx:advice>

十三、 配置中文 ,在web.xml 添加一个filter ,放在structs2 filter 前面
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> 


 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值