使用maven 创建web项目 + 搭建SSM框架
1、右键-new --project - maven project
2、选中默认workspace
3、选择 webapp(创建web项目)
4、设置项目名称
5、创建成功。出错是正常的,现在目录不完整,需要补齐目录
各个目录的作用
6、补全目录
选择 src-main -右击-new-folder-java
选择 src -右击--new-folder-test
选择 src- test - -右击-new-folder-java
选择 src- test - -右击-new-folder-resources
四个文件夹建完,如图
7、更新 。在项目上右击--maven--update project之后,生成test/resources目录
8、选择要更新的项目,点完成
9、至此项目建成,jsp页面出错的原因是因为缺少相关jar包。
10、在pom.xml中添加jar,这里把ssm相关的jar包也直接添加了
<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>com.hp</groupId> <artifactId>MavenSSM</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>MavenSSM Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Spring的jar包 SpringMVC的jar包 Mybatis的jar包 Spring整合mybatis的jar包 Spring整合SpringMVC的jar包 mysql的jar包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <!-- 把list集合转换成json数据的jar包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <!-- FastDFS 文件上传 --> <!-- <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.4</version> </dependency> --> <!-- 文件上传下载 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <!-- 画图 --> <dependency> <groupId>org.jfree</groupId> <artifactId>jcommon</artifactId> <version>1.0.24</version> </dependency> <dependency> <groupId>org.jfree</groupId> <artifactId>jfreechart</artifactId> <version>1.0.19</version> </dependency> <!-- c3p0连接池jar包 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5-pre10</version> </dependency>
<!-- 添加分页查询pagehelper.jar包 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
</dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <configuration> <version>3.0</version> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!-- 配置文件地址 --> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build> </project>
11、再次更新 项目。在项目上右击--maven--update project之后,整个工程无错误。项目创建ok。
12、开始搭建ssm环境。因为之前已经导入过ssm相关的jar了,所以,现在直接配置xml文件。
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 所有以/结尾的请求,都交给Spring MVC处理 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--配置POST请求中⽂乱码过滤器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <!--指定编码 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
在java/resources目录创建这几个文件
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 开启注解扫描,扫描com.hp.controller下的所有@Controller类的注解 --> <context:component-scan base-package="com.hp.controller" /> <!--释放静态资源 --> <mvc:default-servlet-handler /> <!--开启mvc注解⽀持 --> <mvc:annotation-driven /> <!--自定义视图解析器 --> <bean id="viewresolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认编码 --> <property name="defaultEncoding" value="utf-8" /> <!-- ⽂件⼤⼩最⼤值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 内存中的最⼤值 --> <property name="maxInMemorySize" value="40960" /> </bean> <mvc:interceptors> <mvc:interceptor> <!-- 指定当前拦截器拦截的路径 --> <mvc:mapping path="/**" /> <!-- 指定当前拦截器不需要拦截的路径 --> <mvc:exclude-mapping path="/login" /> <mvc:exclude-mapping path="/css/**" /> <mvc:exclude-mapping path="/js/**" /> <!-- 自定义拦截器 --> <bean class="com.hp.interceptor.PowerValidateInterceptor" /> </mvc:interceptor> </mvc:interceptors> </beans>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" > <!-- 扫描注解 只会扫描这个包里面有@service注解的类 --> <context:component-scan base-package="com.hp.service"></context:component-scan> <!-- 加载属性文件 --> <context:property-placeholder location="classpath:db.properties" /> <!--数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 创建sqlsessionFactory --> <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="typeAliasesPackage" value="com.hp.pojo"> </property> <!-- 如果还有⼀些专⻔针对于mybatis的配置,需要引⼊ --> <property name="configLocation" value="classpath:mybatis.xml" /> </bean> <!-- 创建扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.hp.mapper"></property> <property name="sqlSessionFactoryBeanName" value="factory"></property> </bean> </beans>
mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- 设置延迟加载开关,默认false(⽴即加载) --> <setting name="lazyLoadingEnabled" value="false" /> <!-- 开启MyBatis⼆级缓存配置,默认已经开启,可以省略 --> <setting name="cacheEnabled" value="true" /> <!-- 设置驼峰命名规则,会将表字段名user_name⾃动映射到属性名userName --> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> <plugins> <!-- 分页查询工具pagehelper --> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="properties" value="mysql" /> </plugin> </plugins> </configuration>
db.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/oasys jdbc.username=root jdbc.password=xy1234
登录测试
1、login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> <link href="css/bootstrap.min.css" rel="stylesheet"> <link href="css/toastr.min.css" rel="stylesheet"> <script src="js/jquery-1.9.1.js" type="text/javascript"></script> <script src="js/toastr.min.js" type="text/javascript"></script> <script type="text/javascript"> function submit(){ if($('#username').val()==""){ toastr.info("用户名不能为空") return false; } if($('#pwd').val()==""){ toastr.info("密码不能为空") return false; } $.ajax({ url:"login", data:{"name":$('#username').val(),"pwd":$('#pwd').val()}, dataType:"json", success:function (result){ if(result==1){ toastr.success("登录成功") }else{ toastr.error("登录失败") } } }) } </script> </head> <body> 用户名:<input type="text" id="username"><br> 密码:<input type="text" id="pwd"><br> <button onclick="submit()">登录</button> </body> </html>
2.后台代码
1 @Controller 2 public class LoginController { 3 4 @Autowired 5 private LoginService loginservice; 6 7 @RequestMapping("login") 8 @ResponseBody 9 public int login(String name, String pwd,HttpSession session) { 10 User user = loginservice.login(name, pwd); 11 if (user != null) { 12 session.setAttribute("LOGIN_USER", user); 13 return 1; 14 } else { 15 return 0; 16 } 17 } 18 } 19 20 21 public interface LoginService { 22 23 public User login(String name, String pwd); 24 } 25 26 27 @Service 28 public class LoginServiceImpl implements LoginService { 29 30 @Autowired 31 private LoginMapper loginMapper; 32 33 @Override 34 public User login(String name, String pwd) { 35 return loginMapper.login(name, pwd); 36 } 37 38 } 39 40 public class User { 41 private int id; 43 private String uame; 44 private String upwd; 45 private int role; 46 private String sex; 50 51 public int getId() { 52 return id; 53 } 54 55 public void setId(int id) { 56 this.id = id; 57 } 67 public String getUame() { 68 return uame; 69 } 70 71 public void setUame(String uame) { 72 this.uame = uame; 73 } 74 75 public String getUpwd() { 76 return upwd; 77 } 78 79 public void setUpwd(String upwd) { 80 this.upwd = upwd; 81 } 82 83 public int getRole() { 84 return role; 85 } 86 87 public void setRole(int role) { 88 this.role = role; 89 } 90 91 public String getSex() { 92 return sex; 93 } 94 95 public void setSex(String sex) { 96 this.sex = sex; 97 }122 } 123 124 public interface LoginMapper { 125 126 @Select("select * from users where uame=#{uame} and upwd=#{upwd}") 127 User login(@Param("uame") String name, @Param("upwd") String pwd); 128 } 129 130 131 132 public class PowerValidateInterceptor extends HandlerInterceptorAdapter{ 133 134 @Override 135 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 136 throws Exception { 137 HttpSession session = request.getSession(); 138 Object user = session.getAttribute("LOGIN_USER"); 139 System.out.println("拦截路径:"+request.getRequestURL()); 140 if(user!=null){ 141 return super.preHandle(request, response, handler); 142 }else{ 143 response.sendRedirect(request.getContextPath()+"/index.jsp"); 144 return false; 145 } 146 } 147 }
启动项目。第一次使用 5 maven build... ,输入启动参数 tomcat7:run 之后直接运行 4 maven build
正常启动项目。在浏览器里面输入地址,进行测试
完成!!!!!