使用maven搭建web工程

1、新建maven工程,取名xiaoali

在pom.xml文件添加依赖

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>xiaoali</groupId>
  <artifactId>xiaoali</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>xiaoali</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>4.3.9.RELEASE</spring.version>
    <junit.version>4.12</junit.version>
  </properties>

  <dependencies>
    <!-- 首先添加springmvc依赖包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-instrument</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-instrument-tomcat</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-messaging</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc-portlet</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-websocket</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

因为使用maven clean整个工程后会出现jdk版本和自己在eclipse中配置的不一样,这时候需要在pom.xml中添加jdk版本控制插件

<build>
        <finalName>xiaoali</finalName>
        <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <configuration>
                <source>1.7</source>
                <target>1.7</target>
              </configuration>
            </plugin>
        </plugins>
    </build>

然后重新clean就可以了

2、配置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>xiaotb</display-name>
  <!-- 默认欢迎界面 -->
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <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:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
    <url-pattern>/xiaoali/login</url-pattern>
  </servlet-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-context.xml</param-value>
  </context-param>
</web-app>

3、创建spring-context.xml和spring-mvc.xml文件

spring-context.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:p="http://www.springframework.org/schema/p"
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">
<!--    ----------------------------------------------------------
   进行扫描类包、读取配置文件、数据源管理、AOP配置、缓存以及消息队列等配置
   ---------------------------------------------------------- -->

<!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
<context:component-scan base-package="com.xiaoali.controller" />
<context:component-scan base-package="com.xiaoali.service" />
<context:component-scan base-package="com.xiaoali.dao" />
</beans>

spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-4.3.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.3.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

  <!-- 使用默认的注解映射 -->
  <mvc:annotation-driven>
  </mvc:annotation-driven>

</beans>

启动tomcat,报错
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
找不到类,原因是需要在工程中导入maven依赖jar包。导入完成后重启,没有报错信息显示。

4、输入访问地址

http://localhost:8080/xiaoali/login
控制台输出:
警告: No mapping found for HTTP request with URI [/xiaoali/login] in DispatcherServlet with name ‘springMVC’
原因:扫描类包的配置应该配置在spring-mvc.xml文件中,web.xml配置中Spring初始化参数是在spring-mvc.xml中,而不是在spring-context.xml,服务的启动首先加载spring-context.xml中的配置项,然后才初始化springframework

5、package创建

controller层
UserController.java

package com.xiaoali.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xiaoali.entity.User;
import com.xiaoali.service.UserService;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value= "/login")
    @ResponseBody
    public List<User> findUserList(){
        List<User> userList = userService.findUserList();
        for(User user : userList){
            System.out.println(user.getName()+":"+user.getPwd());
        }
        return userList;
    }
}

service层
接口类 UserService.java

package com.xiaoali.service;

import java.util.List;
import com.xiaoali.entity.User;

public interface UserService {

    public List<User> findUserList();
}

实现类 UserServiceImpl.java

package com.xiaoali.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xiaoali.dao.UserDao;
import com.xiaoali.entity.User;
import com.xiaoali.service.UserService;

@Service
public class UserServiceImpl
    implements UserService {

    @Autowired
    private UserDao userDao;
    @Override
    public List<User> findUserList() {
        List<User> userList = userDao.findUserList();
        return userList;
    }

}

dao层
UserDao.java

package com.xiaoali.dao;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.xiaoali.entity.User;

@Repository
public class UserDao {

    private User user;

    public List<User> findUserList(){
        List<User> userList = new ArrayList<User>();
        for(int i =0 ;i<3;i++){
            user = new User();
            user.setName("张三"+i);
            user.setPwd("A"+i);
            userList.add(user);
        }
        return userList;
    }
}

entity层
User.java

package com.xiaoali.entity;

import java.io.Serializable;

public class User implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -5927908541432372896L;
    private String name = "";
    private String pwd = "";
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }


}

6、启动服务输入地址

http://localhost:8080/xiaoali/login
报错:严重: Servlet.service() for servlet [springmvc] in context with path [/xiaoali] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList] with root cause
java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList

原因:这是因为springmvc默认是没有对象转换成json的转换器的,需要手动添加jackson依赖。
解决:手动添加jackson依赖到pom.xml文件中

<jackson.version>2.8.9</jackson.version>

<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.version}</version>
    </dependency>

7、更新项目工程,重新导入maven依赖,重启服务

输入地址http://localhost:8080/xiaoali/login
访问成功,数据显示。
这里写图片描述
如果还是没有解决,在springmvc配置文件中进行如下配置

  <mvc:annotation-driven>
<!--     <mvc:message-converters> -->
<!--             <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> -->
<!--             <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> -->
<!--     </mvc:message-converters> -->
  </mvc:annotation-driven>

我这里注释掉了,因为我不需要。ヾ(◍°∇°◍)ノ゙

  • 后续:整合mybatis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值