使用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.0modelVersion>

  <groupId>xiaoaligroupId>
  <artifactId>xiaoaliartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>jarpackaging>

  <name>xiaoaliname>
  <url>http://maven.apache.orgurl>

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

  <dependencies>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aopartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aspectsartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-expressionartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-instrumentartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-instrument-tomcatartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jmsartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-messagingartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-ormartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvc-portletartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-websocketartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>${junit.version}version>
      <scope>testscope>
    dependency>
  dependencies>
project>

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

<build>
        <finalName>xiaoalifinalName>
        <plugins>
            <plugin>
              <groupId>org.apache.maven.pluginsgroupId>
              <artifactId>maven-compiler-pluginartifactId>
              <configuration>
                <source>1.7source>
                <target>1.7target>
              configuration>
            plugin>
        plugins>
    build>

然后重新clean就可以了

2、配置web.xml文件


<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>xiaotbdisplay-name>
  
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>

  <servlet>
    <servlet-name>springMVCservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-mvc.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>springMVCservlet-name>
    <url-pattern>/url-pattern>
    <url-pattern>/xiaoali/loginurl-pattern>
  servlet-mapping>

  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:spring-context.xmlparam-value>
  context-param>
web-app>

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

spring-context.xml


<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">



<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


<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 findUserList(){
        List 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 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 findUserList() {
        List 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 findUserList(){
        List userList = new ArrayList();
        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.9jackson.version>

<dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-coreartifactId>
      <version>${jackson.version}version>
    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>${jackson.version}version>
    dependency>

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

输入地址http://localhost:8080/xiaoali/login
访问成功,数据显示。
使用maven搭建web工程_第1张图片
如果还是没有解决,在springmvc配置文件中进行如下配置

  <mvc:annotation-driven>




  mvc:annotation-driven>

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

  • 后续:整合mybatis

你可能感兴趣的:(spring-mvc)