zz from : http://www.myexception.cn/web/1476438.html
Maven构建并热部署一个简单的Web Project
第一步:构建Maven环境和安装m2e插件
这一步就不啰嗦了,很简单。照着做就行了
怎么构建Maven环境网上有很多教程,下面我随便找了一个
http://blog.csdn.net/chenxuejiakaren/article/details/7938524
eclipse安装m2e插件
Help -> Install New Software
http://download.eclipse.org/technology/m2e/releases/
第二步:创建Maven工程
File -> New -> Other -> Maven -> Maven Project
在src/main/java下创建包路径,存在就不用创建了
com/msh/produce/crs
为让源文件目录下面的包看上去是包的图标而不是文件夹,可以通过下面步骤修改
邮件工程 -> Build Path -> Link Source 选择 src/main/java 作为源文件目录即可
第三步:给工程添加Spring和Struts2对应jar包依赖
编辑pom.xml文件,添加以下内容
<!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6.SEC03</version> </dependency> <!-- struts2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.15.3</version> </dependency> <!-- struts2-spring-plugin --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.15.3</version> </dependency>
为了方面记录日志,在添加log4j依赖
<!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
如果你需要添加某个依赖,不知道它的groupId,artifactid,version,推荐大家一个搜索jar的站点,类似的站点很多,我就不一一列举了,下面看下例子就行了
好了,spring、struts、log4j的依赖配置都配置好了之后,我们就需要让maven来帮我们来下载这些依赖的jar包了
右键工程 -> Maven -> Update Project
OK,查看下依赖的jar都加进来了没?
是不是 so easy,so simple,比你去网上搜罗这些jar要方便的多了
因为下面我们还要用到MySQL和JSP所以还需要添加一下几个依赖
<!-- dbcp 配置数据源用的--> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.27</version> </dependency> <!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> <scope>provided</scope> </dependency>
OK,再重复下上面的步骤,再看看jar包是不是都有了
好了准备工作做的差不多了,可以开始写代码了
上面第三步还漏了一步,需要将Maven工程设置为Dynamic Web Project
工程右键 -> Project Facets -> 勾选上 Dynamic Web Module 2.5
注意:
如果出现更改不了,可以直接修改SimpleWebProject/.setting/org.eclipse.wst.common.project.facet.core.xml
修改后,重启下eclipse
第四步:编写一个最简单的注册功能
创建如下包路径
=====applicationContext.xml======
<!-- log4j --> <bean id="log4jConfigurer" class="org.springframework.util.Log4jConfigurer" abstract="true" factory-method="initLogging"> <constructor-arg index="0" value="classpath*:log4j.properties"></constructor-arg> <constructor-arg index="1" value="30000"></constructor-arg> </bean> <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc0.driverClassName}"></property> <property name="url" value="${jdbc0.url}"></property> <property name="username" value="${jdbc0.username}"></property> <property name="password" value="${jdbc0.password}"></property> <property name="maxActive" value="${jdbc0.maxActive}"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg ref="dataSource"></constructor-arg> </bean> <!-- regiser module --> <import resource="classpath:com/msh/produce/crs/register/applicationContext_crs_register.xml" />
=====jdbc.properties=====
jdbc0.driverClassName=com.mysql.jdbc.Driver jdbc0.url=jdbc:mysql://localhost:3306/MySQL jdbc0.username=root jdbc0.password=root jdbc0.maxActive=5
=====log4j.properties=====
log4j.rootLogger=info,console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss:SSS} [%p]-[%C.%M:%L] %m%n
=====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> <!-- regiser module --> <include file="com/msh/produce/crs/register/struts_crs_register.xml"></include> </struts>
=====RegisterManagerDAO=====
package com.msh.produce.crs.register.dao; public interface RegisterManagerDAO { String SUCCESS = "注册成功"; String FAILURE = "注册失败"; /** * 保存注册信息 * * @param name * @param passwd * @return */ String saveRegisterInfo(String name, String passwd); } =====RegisterManagerDAOImp===== package com.msh.produce.crs.register.dao; import java.sql.PreparedStatement; import java.sql.SQLException; import org.apache.log4j.Logger; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementSetter; public class RegisterManagerDAOImp implements RegisterManagerDAO { @Override public String saveRegisterInfo(final String name, final String passwd) { try { jt.update("insert into tab_person(name,passwd) values(?,?)", new PreparedStatementSetter() { @Override public void setValues(PreparedStatement ps) throws SQLException { ps.setString(1, name); ps.setString(2, passwd); } }); return SUCCESS; } catch (Exception e) { log.error("", e); return FAILURE; } } public void setJt(JdbcTemplate jt) { this.jt = jt; } private static final Logger log = Logger.getLogger(RegisterManagerDAOImp.class); private JdbcTemplate jt; }
======RegisterManagerAction======
package com.msh.produce.crs.register.action; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.apache.struts2.ServletActionContext; import com.msh.produce.crs.register.dao.RegisterManagerDAO; import com.opensymphony.xwork2.ActionSupport; public class RegisterManagerAction extends ActionSupport { /** * 账号注册页面 * * @return */ public String register() { if (log.isInfoEnabled()) { log.info("Forward to register page!"); } return "register"; } /** * 账号注册 * * @return */ public void doRegister() { if (log.isInfoEnabled()) { log.info("Do register {name=" + name + ", passwd=" + passwd + "}"); } String ajax = registerManagerDao.saveRegisterInfo(name, passwd); writeAjax(ajax); } private void writeAjax(String ajax) { HttpServletResponse hsr = ServletActionContext.getResponse(); try { hsr.reset(); hsr.setContentType("text/html;charset=utf-8"); hsr.getWriter().write(ajax); hsr.getWriter().flush(); hsr.getWriter().close(); } catch (IOException e) { log.error("", e); } } public void setRegisterManagerDao(RegisterManagerDAO registerManagerDao) { this.registerManagerDao = registerManagerDao; } public void setName(String name) { this.name = name; } public void setPasswd(String passwd) { this.passwd = passwd; } private static final long serialVersionUID = -6370332854095254121L; private static final Logger log = Logger.getLogger(RegisterManagerAction.class); private RegisterManagerDAO registerManagerDao; private String name; private String passwd; }
=====applicationContext_crs_register.xml======
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="crs_register_RegisterManagerDAOImp" class="com.msh.produce.crs.register.dao.RegisterManagerDAOImp"> <property name="jt"> <ref bean="jdbcTemplate"/> </property> </bean> <bean id="crs_register_RegisterManagerAction" class="com.msh.produce.crs.register.action.RegisterManagerAction" scope="prototype"> <property name="registerManagerDao" ref="crs_register_RegisterManagerDAOImp"></property> </bean> </beans>
======struts_crs_register.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> <package name="crs_register" extends="struts-default" namespace="/register"> <action name="registerManagerAction" class="crs_register_RegisterManagerAction"> <result name="register">/crs/register/register.jsp</result> </action> </package> </struts>
编写jsp页面
=====register.jsp======
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>注册页面</title> <link rel="stylesheet" type="text/css" href="<s:url value="/ui/css/normal/ui.css"/>" /> <style type="text/css"> </style> <script type="text/javascript" src="<s:url value="/ui/js/jquery-1.4.4.js"/>"></script> <script type="text/javascript"> $(function() { $("#registerBtn").click( function() { $.ajax ({ url:"<s:url value="/register/registerManagerAction!doRegister.action"/>", type:"POST", data: { name:$.trim($("input[name=name]").val()), passwd:$.trim($("input[name=passwd]").val()) }, success:function(data) { $("#registerResult").html(data); }, error:function(err){} }); } ); }); </script> </head> <body> <table class="propertygrid" style="width:30%"> <tr> <th width="40%">账号:</th> <td> <input type="text" name="name" /> </td> </tr> <tr> <th width="40%">密码:</th> <td> <input type="password" name="passwd" /> </td> </tr> <tr> <th colspan="2" style="text-align:center;"> <input id="registerBtn" type="button" value="注册" /> </th> </tr> </table> <div id="registerResult" style="width:30%;"></div> </body> </html>
代码都写好了,下面需要部署测试功能了
第五步:部署测试
右键工程 -> Deployment Assembly
前面三个是默认的,不需要修改,我们需要新增最后一个lib,因为默认jar都在maven的本地仓库目录中,热部署的时候我们需要将jar全部拷贝到WEB-INF/lib下
给eclipse配置Server Runtime Environment
window -> Preferences -> Server -> Runtime Environments
将你本地安装的tomcat添加进来
在eclipse中增加一个run on server
window -> show view -> other... -> Server -> Servers
访问下页面测试下
完成了,是不是很简单。