`
aaagu1234
  • 浏览: 144565 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

用m2clipse构建appfuse-basic-struts-archetype(v2.1.0-M1)

阅读更多
1、新建一Maven project工程。选择Archetype时选取appfuse-basic-struts-archetype(v2.1.0-M1),在填写maven坐标时使包名与Group Id相同(如果不同在用appfuse自动生成Pojo,dao,service代码时包名会不一样,即自动生成代码时appfuse默认会用Group Id作为主包名)。在成功生成初始工程后系统会报错,,此错误可能是appfuse的bug,默认会多一层包(即在会在用户指定包名后多了一个".webapp",但生成类的代码中的包名为正确),解决办法,将src/main/java和src/test/java下包多出的包名部分从物理硬盘中删除对应的文件夹,注意不能用eclipse修改包名的方法删除,这样会报错的。

2、打开pom.xml文件修改对应的数据库连接配置,然后解决appfuse2中displaytag中文乱码问题,具体修改对应代码为如下(红字为修改部分):
<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>native2ascii-maven-plugin</artifactId>
                <version>1.0-alpha-1</version>
                <configuration>
                    <dest>target/classes</dest>
                    <src>src/main/resources</src>
                </configuration>
                <executions>
                    <execution>
                        <id>native2ascii-utf8</id>
                        <goals>
                            <goal>native2ascii</goal>
                        </goals>
                        <configuration>
                            <encoding>UTF8</encoding>
                            <includes>
                                ApplicationResources_ko.properties,
                                ApplicationResources_no.properties,
                                ApplicationResources_tr.properties,
                                *_zh*.properties
                            </includes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>native2ascii-8859_1</id>
                        <goals>
                            <goal>native2ascii</goal>
                        </goals>
                        <configuration>
                            <encoding>8859_1</encoding>
                            <includes>
                                ApplicationResources_de.properties,
                                ApplicationResources_fr.properties,
                                ApplicationResources_nl.properties,
                                ApplicationResources_pt*.properties
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

以及——

<resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>ApplicationResources_de.properties</exclude>
                    <exclude>ApplicationResources_fr.properties</exclude>
                    <exclude>ApplicationResources_ko.properties</exclude>
                    <exclude>ApplicationResources_nl.properties</exclude>
                    <exclude>ApplicationResources_no.properties</exclude>
                    <exclude>ApplicationResources_pt*.properties</exclude>
                    <exclude>ApplicationResources_tr.properties</exclude>
                    <exclude>*_zh*.properties</exclude>
                    <exclude>applicationContext-resources.xml</exclude>
                    <exclude>struts.xml</exclude>
                </excludes>
                <filtering>true</filtering>
            </resource>

3、用cmd进入工程对应文件夹并运行mvn下载所有的包和文件 和mvn jetty:run-war 可通过http://localhost:8080访问程序

4、如果不想用appfuse的jar包可运行 mvn appfuse:full-source,此命令会将appfuse中的源代码抽出来,并将包名修改为你项目的包名,同时将源代码放入项目的源码文件夹中,这样所有你项目中用到的appfuse类都变成了你自己的类。同时会修改pom.xml以适合你的工程,修改后属性amp.fullSource的值为 <amp.fullSource>true</amp.fullSource>,此属性表示此工程已运行过mvn appfuse:full-source,即不再依赖appfuse的包。如果在运行命令时报像“''svn: PROPFIND request failed on'' ”的错则在org.appfuse.plugins的节点<configuration>下加上<trunk>https://svn.java.net/svn/appfuse~svn/</trunk>

5、由表生成model (pojo),然后由model 生成action、service、dao和对应的页面,以及设置相应的配置文件。首行创建表employee
CREATE TABLE `Employee` (
`id` bigint(20) NOT NULL auto_increment,
`code` varchar(10) NOT NULL,
`dept` varchar(50) NOT NULL,
`name` varchar(20) NOT NULL,
`status` varchar(10) NOT NULL,
`telephone` varchar(20) default NULL,
`title` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
然后运行命令mvn appfuse:gen-model 会在model包中生成Employee.java,但是会报错, 查看原代码,原来model中有两个ID的生成策略,如下:
@Id @GeneratedValue(strategy=IDENTITY) @GeneratedValue(strategy = GenerationType.AUTO)
删除@GeneratedValue(strategy=IDENTITY),只剩下一个。
接下来是由model 生成CRUD:在项目下的pom.xml中查找genericCore,找到 将属性true 改为false,不修改默认将不会生成dao和service,然后运行命令mvn appfuse:gen -Dentity=Employee 将会生成相应的employeeList.jsp、employeeForm.jsp、EmployeeAction.java 和对应的dao、service。运行 mvn jetty:run 在浏览器中查看界面吧,相应连接都已在界面中。


6、配置eclipse运行调试环境。打开eclipse并刷新工程文件,然后选择菜单栏run下的run Configurations..进入运行设置窗口。新建一Maven Build设置,命名为mvn jetty run,在Base directory中选择或填写对应的工程,然后在Goals中填入命令jetty:run,并勾选下面的Skip Tests(此项可在运行时跳过测试,也在在pom.xml 的属性中新建<skipTests>true</skipTests>),确定运行。配置完后即可在pom.xml上右击,选择run呀debug中的maven build,然后选择mvn jetty run运行或调试启动jetty.

7、常用的命令介绍:
------------------------------------------------------------------------------------------

mvn war:inplace 将war中的文件放入 自己的项目的webapp目录
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       编译并发布你的应用程序到Jetty, 查看在 http://localhost:8080
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
start C:\Progra~1\Java\jdk1.6.0\bin\native2ascii -encoding UTF-8 d:\workspace\dayawa\src\ApplicationResources_zh.properties d:\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 newusername throw 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;





  12、如果要跳过单元测试在<properties>添加 <skipTests>true</skipTests>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics