此文已由做者趙計剛受權網易雲社區發佈。html
歡迎訪問網易雲社區,瞭解更多網易技術產品運營經驗。java
2.2.三、ssmm0-userManagementweb
pom.xmlspring
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4 5 <modelVersion>4.0.0</modelVersion> 6 7 <!-- 指定父模塊 --> 8 <parent> 9 <groupId>com.xxx</groupId> 10 <artifactId>ssmm0</artifactId> 11 <version>1.0-SNAPSHOT</version> 12 </parent> 13 14 <groupId>com.xxx.ssmm0</groupId> 15 <artifactId>ssmm0-userManagement</artifactId> 16 <!--<version>1.0-SNAPSHOT</version>--><!-- 父模塊已經指定了版本號,這裏就不用了--> 17 18 <name>ssmm0-userManagement</name> 19 <packaging>war</packaging><!-- 須要部署的模塊 --> 20 21 <!-- 引入實際依賴 --> 22 <dependencies> 23 <!-- 將ssmm0-data項目做爲一個jar引入項目中 --> 24 <dependency> 25 <groupId>com.xxx.ssmm0</groupId> 26 <artifactId>ssmm0-data</artifactId> 27 <version>1.0-SNAPSHOT</version> 28 </dependency> 29 <!-- servlet --> 30 <dependency> 31 <groupId>javax.servlet</groupId> 32 <artifactId>javax.servlet-api</artifactId> 33 </dependency> 34 <!-- spring mvc --> 35 <dependency> 36 <groupId>org.springframework</groupId> 37 <artifactId>spring-web</artifactId> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-webmvc</artifactId> 42 </dependency> 43 <!-- 這個是使用velocity的必備包 --> 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-context-support</artifactId> 47 </dependency> 48 <!-- velocity --> 49 <dependency> 50 <groupId>org.apache.velocity</groupId> 51 <artifactId>velocity</artifactId> 52 </dependency> 53 <dependency> 54 <groupId>velocity-tools</groupId> 55 <artifactId>velocity-tools-generic</artifactId> 56 </dependency> 57 </dependencies> 58 </project>
注意:將ssmm0-data做爲普通的jar引入便可。sql
spring.xml數據庫
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-3.2.xsd 9 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 10 11 <!-- 註解掃描 --> 12 <context:component-scan base-package="com.xxx" /> 13 14 <!-- 配置fastjson轉換器 --> 15 <mvc:annotation-driven> 16 <mvc:message-converters register-defaults="true"> 17 <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"></bean> 18 </mvc:message-converters> 19 </mvc:annotation-driven> 20 21 <!-- 引入數據源,這裏變量的讀取都是從ssmm0的pom.xml中讀取的 --> 22 <bean id="xxxDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> 23 <property name="driverClassName" value="${jdbc.driverClassName}" /> 24 <property name="url" value="${jdbc.url}" /> 25 <property name="username" value="${jdbc.username}" /> 26 <property name="password" value="${jdbc.password}" /> 27 </bean> 28 29 <!-- 引入mybatis --> 30 <bean id="xxxSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 31 <property name="dataSource" ref="xxxDataSource" /> 32 </bean> 33 <bean id="xxxMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 34 <!-- 35 這裏就是包名爲何就作com.xxx.mapper.user而非com.xxx.user.mapper, 36 這樣的話,好比說有兩個項目com.xxx.mapper.user和com.xxx.mapper.hotel,value只需寫做com.xxx.mapper便可 37 不然,value就要寫做com.xxx.user.mapper,com.xxx.hotel.mapper 38 --> 39 <property name="basePackage" value="com.xxx.mapper" /> 40 <property name="sqlSessionFactoryBeanName" value="xxxSqlSessionFactory" /> 41 </bean> 42 43 <!-- 配置velocity --> 44 <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 45 <property name="resourceLoaderPath"> 46 <value>WEB-INF/templates/</value> 47 </property> 48 <property name="velocityProperties"> 49 <props> 50 <prop key="input.encoding">UTF-8</prop> 51 <prop key="output.encoding">UTF-8</prop> 52 </props> 53 </property> 54 </bean> 55 <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> 56 <property name="suffix" value=".vm" /> 57 <property name="contentType" value="text/html;charset=utf-8" /> 58 <property name="dateToolAttribute" value="date"/> 59 <property name="numberToolAttribute" value="number"/> 60 </bean> 61 </beans>
注意:這裏對包名的體現,因爲直接使用spring.xml去讀ssmm0的 pom.xml了,因此jdbc.properties文件就不要了,在spring.xml中,指定文件位置的<context:property-placeholder>標籤就刪掉了apache
web.xmljson
1 <?xml version="1.0" encoding="utf-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 5 6 <servlet> 7 <servlet-name>dispatcherServlet</servlet-name> 8 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 9 <init-param> 10 <param-name>contextConfigLocation</param-name> 11 <param-value>classpath:spring.xml</param-value> 12 </init-param> 13 <load-on-startup>1</load-on-startup> 14 </servlet> 15 <servlet-mapping> 16 <servlet-name>dispatcherServlet</servlet-name> 17 <url-pattern>/</url-pattern> 18 </servlet-mapping> 19 20 <filter> 21 <filter-name>encodingFilter</filter-name> 22 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 23 <init-param> 24 <param-name>encoding</param-name> 25 <param-value>UTF-8</param-value> 26 </init-param> 27 <init-param> 28 <param-name>forceEncoding</param-name> 29 <param-value>true</param-value> 30 </init-param> 31 </filter> 32 <filter-mapping> 33 <filter-name>encodingFilter</filter-name> 34 <url-pattern>/*</url-pattern> 35 </filter-mapping> 36 37 <welcome-file-list> 38 <welcome-file>/index.jsp</welcome-file> 39 </welcome-file-list> 40 </web-app>
com.xxx.web.admin.AdminControllerapi
1 package com.xxx.web.admin; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestParam; 7 import org.springframework.web.bind.annotation.ResponseBody; 8 import org.springframework.web.servlet.ModelAndView; 9 10 import com.xxx.model.userManagement.Admin; 11 import com.xxx.service.userManagement.AdminService; 12 13 /** 14 * adminController 15 */ 16 @Controller 17 @RequestMapping("/admin") 18 public class AdminController { 19 20 @Autowired 21 private AdminService adminService; 22 23 /** 24 * 管理員註冊 25 */ 26 @ResponseBody 27 @RequestMapping("/register") 28 public boolean register(@RequestParam("username") String username, 29 @RequestParam("password") String password){ 30 Admin admin = new Admin(); 31 admin.setUsername(username); 32 admin.setPassword(password); 33 34 boolean isRegisterSuccess = adminService.register(admin); 35 36 return isRegisterSuccess; 37 } 38 39 /** 40 * 管理員登陸 41 */ 42 @RequestMapping("/login") 43 public ModelAndView login(@RequestParam("username") String username, 44 @RequestParam("password") String password){ 45 Admin admin = adminService.login(username, password); 46 47 ModelAndView modelAndView = new ModelAndView(); 48 if(admin == null){ 49 modelAndView.addObject("message", "用戶不存在或者密碼錯誤!請從新輸入"); 50 modelAndView.setViewName("error"); 51 }else{ 52 modelAndView.addObject("admin", admin); 53 modelAndView.setViewName("userinfo"); 54 } 55 56 return modelAndView; 57 } 58 }
error.vmspring-mvc
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>登陸失敗</title> 6 </head> 7 <body> 8 <div> 9 $message 10 </div> 11 </body> 12 </html>
userinfo.vm
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>登陸成功</title> 6 </head> 7 <body> 8 <div> 9 id:$admin.id 10 username:$admin.username 11 password:$admin.password 12 </div> 13 </body> 14 </html>
具體測試過程:
直接運行jetty的話,會發現連不上數據庫服務器99.99.99.999,這是由於默認啓動服務器以後,咱們使用的是prod的一套env,這套env中的數據庫服務器是我本身亂寫的:99.99.99.999
這時候,在ssmm0-userManagement項目上右擊-->"Build Path"-->"Configure Build Path"-->"Maven"-->修改env,以下圖所示,這樣就切換到了dev的env下,以後再運行jetty,若是依舊不行的話,就採用ssmm0的pom.xml文件中我寫的那塊註釋的方法。
這樣,一個基本上就是企業中開發經常使用的結構的項目就完成了,這個項目很是重要,必定本身試着去寫一個,必定要仔細看其中的每一條註釋。
免費領取驗證碼、內容安全、短信發送、直播點播體驗包及雲服務器等套餐
更多網易技術、產品、運營經驗分享請點擊。
相關文章:
【推薦】 深情留不住,套路得人心--聊聊套路那些事兒
【推薦】 Bug是一種財富-------研發同窗的錯題集、測試同窗的遺漏用例集
【推薦】 PaaS服務之路漫談(三)