APPFUSE2使用说明及常见问题解决方法

来源:互联网 发布:五十知天命的意思 编辑:程序博客网 时间:2024/05/01 17:56
APPFUSE2使用说明及常见问题解决方法
配置好MAVEN_HOME,JAVA_HOME 这个就不废话了:P

------------------------------------------------------------------------------------------
步骤:(以下步均为在命令行的输入)
------------------------------------------------------------------------------------------
第一步 通过APPFUSE创建基于Struts 2 Basic的项目:
mvnarchetype:create -DarchetypeGroupId=org.appfuse.archetypes-DarchetypeArtifactId=appfuse-basic-struts-DremoteRepositories=http://static.appfuse.org/releases-DarchetypeVersion=2.0.1 -DgroupId=com.yourproject-DartifactId=yourproject
执行完毕后CD到yourproject目录中
 
第二步:加入ECLIPSE工程文件,方便导入到ECLIPSE
mvn eclipse:eclipse
 
第三步:修改yourproject/pom.xml 配置文件
   1.修改生成代码方式,找到
      <genericCore>${amp.genericCore}</genericCore>
      <fullSource>${amp.fullSource}</fullSource>
      修改成:
       <genericCore>false</genericCore>
      <fullSource>true</fullSource>
      注意,这里必须吧pom.xml中的genericCore属性设为false 否则只会生成Action
   2.数据库配置信息(mysql在最底下)

第四步:生成MODEL类
先执行,这样会把默认需要的相关类先生成
mvn appfuse:full-source
然后执行
mvn appfuse:gen-model
 
第五步:修改/src/main/resources/hibernate.cfg.xml添加需要生成dao,service,action,jsp的模块
 
第六步:生成Dao,Service,Action
mvn appfuse:gen -Dentity=你的MODEL类
    这里可能会有一个提示找不到/src/main/resources/database.properties,  可以不用理会
  如果设置了很复杂的关联,这个生成过程会很痛苦,老是报targeting an unmapped的错,
  我的解决方法是找到关联的类比如USER,先把关系映射方法和属性删掉,
  然后在再把/src/main/resources/hibernate.cfg.xml其他的类声明删掉
  生成成功后再把这些加上去.
第七步: 把生成的配置文件写入到src目录下的配置文件中
mvn appfuse:install
最后执行mvn jetty:run-war 即可打包并运行项目

打完收工
 
总结:DAO和service的配置文件会生成到:/src/main/webapp/applicationContext.xml中
      struts的配置文件会生成到:/src/main/resources/struts.xml中
------------------------------------------------------------------------------------------
常用的命令介绍:
------------------------------------------------------------------------------------------
mvn appfuse:gen-model    根据数据库的表生成java类
mvn appfuse:gen          根据 POJOs.生成并安装 Tests, DAOs, Managers, Controllers and Views
mvn appfuse:full-source  把运行所需要的org.appfuse中的依赖类转换成你的包名称
mvn eclipse:eclipse      生成eclipse的项目的配置文件,用户可以直接把项目导入到eclipse中
mvn jetty:run-war        打包并且发布你的应用程序到Jetty, 查看在 http://localhost:8080
mvn appfuse:install      把生成的源代码及配置文件写入到src中
mvn integration-test     启动TOMCAT(或别的服务器)进行测试
mvn appfuse:remove       删除appfuse:gen.生成的代码
mvn appfuse:clean        清除target下的所有内容
------------------------------------------------------------------------------------------
需要注意的问题:
------------------------------------------------------------------------------------------
1.必须吧pom.xml中的genericCore属性设为false 否则只会生成Action

3.运行后出现gzip  该问题是压缩HTML的包导致,暂时屏蔽掉相关filter
 
4.使用ehcache1.3以前的版本生成的代码界面字符会变成问号,这是因为maven无法下载正确的ehcache包造成,解决方法是修改/pom.xml ,在里面加上:
 <dependency>
           <groupId>net.sf.ehcache</groupId>
           <artifactId>ehcache</artifactId>
           <version>${ehcache.version}</version>
       </dependency>
       <ehcache.version>1.4.1</ehcache.version>
后再生成代码
 
5.当某张数据表被修改需要更新相应的model时,只需执行mvn appfuse:gen -Dentity=yourmodel
  当添加新的数据表时间,除了执行mvn appfuse:gen -Dentity=yourmodel外,还需要执行
  mvn appfuse:install 来把DAO,STRUTS配置写入到SRC下的相应文件中.
 
6.如果希望生成的model类不在默认的model包根下,只需要在resources/hibernate.cfg.xml中修改想要生成的包路径就行,比如:
默认情况下UserInfo会生成到com.my.app.model下,把配置改成
<mapping class="com.my.app.model.user.UserInfo"/>
UserInfo会生成到com.my.app.model.user下。
 
7.使用Candy for Appfuse 插件时发现model都无法生成,
这是因为%MAVEN_HOME%/conf/settings.xml 中localRepository属性默认为~/.m2/repository
~即当前用户在系统中的用户目录,比如WINDOWS在c:/Documents and Settings/username
因为路径中包含空格,所以Candy找不到,解决方法就是在命令行操作
 
8.关于乱码的问题
对于APPFUSE生成的工程运行时乱码是很常见的现象,造成该现象的原因也有几种:
为了偷懒,这里引用一篇网上的文章:http://www.blogjava.net/43880800/archive/2006/11/18/81892.html
 
对于国际化文件*.properties文件的编辑,有两个ECLIPSE插件可以推荐:Properties Editor 和 ResourceBundleEditor 
再次偷懒引用一篇网文:http://blog.csdn.net/lmjq/archive/2007/06/21/1660137.aspx
 
不过我的IDE有点问题,这两个都装上却不好使,不知道是怎么回事。用了个笨方法:写个批处理来做。内容如下:
echo begin encoding
startC:/Progra~1/Java/jdk1.6.0/bin/native2ascii -encoding UTF-8d:/workspace/dayawa/src/ApplicationResources_zh.propertiesd:/workspace/dayawa/src/ApplicationResources_zh_CN.properties
echo done!
 
保存成 encodeProperties.bat 执行就行,当然,相关路径要设置成你的实际路径。
 
  9. 执行“mvn jetty:run-war”时,会根据POJO先删除数据库里的表再重建,如果不想对数据进行操作可以修改pom.xml,将<configuration>下的drop熟悉修改为 false。不过执行“mvn jetty:run-war”仍然会执行建表操作,出现大量的错误日志,不过没有影响。
 
  10. 执行“mvn jetty:run-war”时,会清空数据表的数据并插入默认的数据,默认的数据在%PROJECT_HOME%/src/main/ resources/default-data.xml配置,这个很讨厌。修改pom.xml中的<dbunit.operation.type>的配置为NONE可以屏蔽这部分操作。
 
  11.在注册新用户时,如果用户名或邮件已经被注册,返回表单改后提交报DataIntegrityViolationException, 这是appfuse的一个BUG,解决方法:
     
    修改UserSecurityAdvice.before()
        在最后的else里加上:user.setVersion(null);
 
    修改UserManagerImpl.saveUser():把最后的try改成:
     try {
            return dao.saveUser(user);
        } catch (DataIntegrityViolationException e) {
           throw new UserExistsException(e.getCause().getCause().toString());
        } catch (EntityExistsException e) { // needed for JPA
            throw new UserExistsException(e.getCause().getCause().getMessage());
        }
     然后在SignupAction.save()改成:
     if(user==null){
      this.addActionMessage(getText("signup.welcome"));
      return INPUT;
      
     }
        user.setEnabled(true);
        //user.setRegistTime(new Date());
        // Set the default user role on this new user
        user.addRole(roleManager.getRole(Constants.USER_ROLE));
       
        try {
            userManager.saveUser(user);
        } catch (AccessDeniedException ade) {
            // thrown by UserSecurityAdvice configured in aop:advisor userManagerSecurity
            log.warn(ade.getMessage());
            getResponse().sendError(HttpServletResponse.SC_FORBIDDEN);
            ade.printStackTrace();
            return null;
        } catch (UserExistsException e) {
            log.warn("message  "+e.getMessage());
            List<String> args = new ArrayList<String>();
           
            if(e.getMessage().indexOf("'"+user.getUsername()+"'")!=-1){
             args.add(user.getUsername());
             addActionError(getText("errors.existing.username", args));
            }else{
             args.add(user.getEmail());
             addActionError(getText("errors.existing.email", args));
            }          
            // redisplay the unencrypted passwords
            user.setPassword(user.getConfirmPassword());
           //to fix the bug that is when username exists and changed newusernamethrow Incorrect result size: expected 1, actual 0 Exception
           
            user.setVersion(null);
            //e.printStackTrace();
            return INPUT;
        }
        saveMessage(getText("user.registered"));
        getSession().setAttribute(Constants.REGISTERED, Boolean.TRUE);
        // log user in automatically
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
                user.getUsername(), user.getConfirmPassword(), user.getAuthorities());
        auth.setDetails(user);
        SecurityContextHolder.getContext().setAuthentication(auth);
        // Send an account information e-mail
        mailMessage.setSubject(getText("signup.email.subject"));
    
        try {
            sendUserMessage(user, getText("signup.email.message"), RequestUtil.getAppURL(getRequest()));
        } catch (MailException me) {
            addActionError(me.getCause().getLocalizedMessage());
            me.printStackTrace();
        }
        return SUCCESS;
原创粉丝点击