分類  >  Web前端 >

Maven構建並熱部署一個簡略的Web Project

tags:    時間:2013-12-09 23:26:30
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

























訪問下頁面測試下











完成了,是不是很簡單。







源碼:

推薦閱讀文章

Bookmark the permalink ,來源:互聯網