开篇之前先讲讲什么是Maven:
Maven 是Apache下的一个开源项目,它是一个创新的项目管理工具,它用于对Java项目进行项目构建、依赖管理及项目信息管理。
总结一下:Maven是个项目管理工具。这个管理工具到底如何使用,有什么用呢?这个才是要说的。在没有maven之前,我们创建一个项目工程或开发中需要依赖某个Jar,都是去下载Jar然后粘贴到当前项目 lib中来,然后每个项目的 lib文件夹下都保存着一堆Jar,然后复制给同事某个项目一看都要几十MB;而使用Maven构建的项目会在项目根目录下生成一个pom.xml, 这个xml文件通过
回到主题,使用 idea创建一个Maven Webapp:
一、创建项目
我不想一个项目换个文件夹(workspace),所以我都是创建Module,关于这一点(idea的Project、Module 和eclipse的workspace 、Project的对应关系)的解释可以参考我上一篇博客:https://blog.csdn.net/shenju2011/article/details/103250717
选择Maven ----> Create form archetype ----> 选择 Maven-archetype-webapp ,如下图是红色选项(不是浅蓝色蓝色),不要选错。
之后填写项目名称,选择项目地址即可:
finish之后会在工程里面生成一个空的项目:
这会有一个build的过程。右下角有进度。构建完成后,目录:
可以看到此时的pom.xml,只引入了一个junit包。如下图:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
二、创建配置文件
先创建文件,下一步再做配置。
创建:app-config.xml(applicationContext.xml), mvc-config.xml,创建Java Package:com.mimi.controller、 com.mimi.domain、com.mimi.service ,UserController , User, UserService ,util等java类文件
三、配置设置
1.pom.xml 引入Jar
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2、配置web.xml
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
3、app-config.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd "> jndi-name="jdbc/AppService" cache="true" resource-ref="true" lookup-on-startup="false" proxy-interface="javax.sql.DataSource"/>
4、mvc-config.xml
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"> json=application/json xml=application/xml html=text/html
5、Java类
UserController.java
package com.mimi.controller;
import com.mimi.domain.BaseResp;
import com.mimi.domain.User;
import com.mimi.service.UserService;
import com.mimi.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
UserService userService;
@Autowired(required = true)
private HttpServletRequest request;
@RequestMapping("/login")
@ResponseBody
public BaseResp login(String userId, String pwd, @ModelAttribute("clientIpAddress") String clientIpAddress)
{
try {
System.out.println("userId : "+userId +"pwd : " + pwd);
// 校验用户名,密码,检验码,设置最后登陆时间和IP地址
HttpSession session = request.getSession();
User user = this.userService.checkLogin(userId, pwd);
System.out.println(user);
if (Util.isBlank(user)) {
// 记录日志
return BaseResp.getFailInstance("Account or Password error, please check your account.");
}
// 获取岗位等其他信息
session.setAttribute("user", user);
// session.setAttribute("emp", emp);
// session.setAttribute("orgName", orgName);
// session.setAttribute("rndNum", userId + sDate);
session.setAttribute("clientIpAddress", clientIpAddress);
// 记录日志
return BaseResp.getSuccessInstance(user);
} catch (Exception e) {
e.printStackTrace();
return BaseResp.getFailInstance();
}
}
}
User.java
package com.mimi.domain;
public class User {
private String userId;
private String password;
private String status;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "User{" +
"userId='" + userId + '\'' +
", password='" + password + '\'' +
", status='" + status + '\'' +
'}';
}
}
BaseResp.java
package com.mimi.domain;
public class BaseResp {
public final static Integer HTTP_OK = 200;
public final static String MESSAGE_OK = "SUCCESS";
public final static Integer HTTP_ERROR_500 = 500;
public final static String MESSAGE_ERROR = "The system error, please wait.";
public final static Integer HTTP_ERROR_501 = 501;
private Integer retCode;
private String message;
private Object data;
public BaseResp() { }
public BaseResp(int retCode, String message, Object data) {
this.retCode = retCode;
this.message = message;
this.data = data;
}
public static BaseResp getSuccessInstance() {
return new BaseResp(HTTP_OK, MESSAGE_OK, null);
}
public static BaseResp getSuccessInstance(Object data) {
return new BaseResp(HTTP_OK, MESSAGE_OK, data);
}
// 系统异常
public static BaseResp getFailInstance() {
return new BaseResp(HTTP_ERROR_500, MESSAGE_ERROR, null);
}
// 业务异常
public static BaseResp getFailInstance(String errorMsg) {
return new BaseResp(HTTP_ERROR_501, errorMsg, null);
}
public Integer getRetCode() {
return retCode;
}
public void setRetCode(Integer retCode) {
this.retCode = retCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
UserService.java
package com.mimi.service;
import com.mimi.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import javax.sql.DataSource;
@Service
public class UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public User checkLogin(String userId, String pwd) {
try {
String sql = "select * from T_USER where userId=? and password=?";
System.out.println("sql="+sql);
final Object[] params = new Object[]{userId, pwd};
return this.jdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper
} catch (EmptyResultDataAccessException e) {
return null;
}
}
}
6、context.xml
这个是数据库连接参数的配置,这里就不说怎么生成这个context.xml,不知道的话可以看看我上一篇文章。
maxActive="20" maxIdle="3" name="jdbc/AppService" password="root123456" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/myfb?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true" username="root"/> 四、数据库、Tomcat的配置 和上一篇使用的是同一个数据库,访问的是同一张表。 Tomcat的配置,就是不说了,上篇文章已经说过了。 配置完Tomcat启动成功后会在 浏览器看到hello word。这里也不做前端页面了,使用一下 postman验证一下登陆接口: 总结: 其实 使用maven来构建项目,就是将Jar交给 pom.xml来引入,然后项目的目录结构有稍微的区别。还有就是在 Idea中使用Maven来构建,他会自动配置 Project Structure,不用我们再像之前那样手动的设置 Modules 、Facets、Artifacts等配置信息。其他的和之前我写过的 简单的apringMva的 Java Web一样。
温度符号怎么打出来?摄氏度℃/华氏度 ℉怎么打出来?
全国鹅的品种大全及生产性能都在这