[使用心得]maven2之m2eclipse使用手册之六使用Maven2插件创建一个简单的SSH2项目之jetty篇(二)
这一篇也是跟jetty插件没有任何关系的,主要是贴出相关整合的struts2的简单代码,框架采用hibernate3.5.3+spring3.0.3+struts2.1.8.1,也就是大家常见的ssh2了,至于spring+hibernate采用的是使用手册之四的通用dao,文章地址:
[使用心得]maven2之m2eclipse使用手册之四编写通用Dao实例(二)
[使用心得]maven2之m2eclipse使用手册之四编写通用Dao实例(三)
[使用心得]maven2之m2eclipse使用手册之四编写通用Dao实例(四)
下面主要贴出sturt2部分的代码与pom.xml的内容,
pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.lxh.ssh</groupId> <artifactId>SSH2Maven</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SSH2Maven JEE5 Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.0.3.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.0.3.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.1.8.1</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.5.3-Final</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.0.Final</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.5.8</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.9.0.GA</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>20030203.000129</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>20030825.184428</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>20030825.183949</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.12</version> <type>jar</type> <scope>runtime</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <type>jar</type> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <type>jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> <type>jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.0.3.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.0.3.RELEASE</version> <type>jar</type> <scope>test</scope> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.1</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.1.8.1</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> <type>jar</type> <scope>compile</scope> </dependency> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>utf-8</encoding> </configuration> </plugin> <finalName>SSH2Maven</finalName> </build> </project>整合ssh2必须的2个jar.commons-file-upload.jar,struts2-spring-plugin这2个如果缺少了这2个的话会出现空指针错误,由于maven的远程服务器还没更新到spring3.03与hibernate3.5.3,所以需要自己手动添加到本地仓库,详细请参考以下文章:
[使用心得]maven2之m2eclipse使用手册之五添加新的依赖项目到本地仓库
注意:maven-compiler-plugin必须在新建好目录后修改成1.6的版本否则,会出现一些bug如@override不能使用,因为这个是选择编译版本的
Struts2
@Controller //@Scope("prototype") public class UserAction implements Action { private UserServiceIf userService; private Collection<User> users; public UserServiceIf getUserService() { return userService; } @Resource(name = "userService") public void setUserService(UserServiceIf userService) { this.userService = userService; } @Override public String execute() { try { users= userService.listAllUser(); return SUCCESS; } catch (Exception e) { e.printStackTrace(); return ERROR; } } public Collection<User> getUsers() { return users; } public void setUsers(Collection<User> users) { this.users = users; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="user" namespace="/" extends="struts-default"> <action name="userinfolist" class="org.lxh.action.UserAction"> <result name="success">/list.jsp</result> <result name="error">/error.html</result> </action> </package> </struts>
jsp:
list.jsp(只贴出部分):
<s:property value="users[0].age" /> <table> <s:iterator var="bb" value="users" > <tr> <td><s:property value="#bb.age" /></td> </tr> </s:iterator> </table>