maven+springmvc的配置

1. 首先创建1个mavenweb项目  如果没有的话最好是去官网下载一个最新版本的eclipse  里面什么都有 maven/gradle 啥的

maven+springmvc的配置

2. 选择路径   没啥影响 就是一个路径 默认就行

maven+springmvc的配置

3. 这里一定要选择  webapp  web项目么 怎么能不选择webapp呢

maven+springmvc的配置

4.  这里就是起名字了

groupId一般分为多个段,第一段为域,第二段为公司名称,这里就 com.demo 吧,举个例子

ArtifactID   就是项目根目录的名称。 我们这是springmvc 就叫springmvc吧

maven+springmvc的配置

5. 到这里项目就建好了  但是我们还要注意 新建出来的肯定会报错因为 jdk版本不对和tomcat没有添加     右击项目springmvc-->properties

1)  看看这两个有没有勾选

maven+springmvc的配置

2) java build path 配置好    这里jdk要edit   tomcat要 Add Libary-->Server Runtime

maven+springmvc的配置

6.  这个时候 项目上面应该没有红叉了  神清气爽   下面就是配置了

1) 首先给大家看下完整的项目目录

maven+springmvc的配置

2)  既然是maven 那么首先肯定是在pom.xml中添加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.ding</groupId>
  <artifactId>springmvc</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springmvc Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <spring.version>4.3.9.RELEASE</spring.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <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-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-jdbc</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>
<!-- commons jar -->

    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.31</version>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
    <version>1.0.19</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>springmvc</finalName>
  </build>
</project>

3) jar加完了 springmvc的第一步肯定是dispatcher   去配置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_2_5.xsd" version="2.5">
  <display-name>Archetype Created Web Application</display-name>
  <!-- 首先是springmvc的 DispatcherServlet -->
  <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>

  <!-- 这里配置了log4j 打印日志   这里配置了context后一定要记得配置对应的listener    我是在配置mybatis的时候因为没有配置对应的监听listener    dao方法无法注入   找了1整整一天  血的教训啊  -->
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>

  <!-- 这个是为Servlet定制URL 也就是拦截什么样的前台请求 -->
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- 首页 -->
  <welcome-file-list>
    <welcome-file>/login.jsp</welcome-file>
  </welcome-file-list>
</web-app>

4)  下面配置 DispatcherServlet 中的springmvc  和log4jConfigLocation 的log4j.properties   还有就是springmvc用到的数据库参数  dbconfig.properties

一.   dbconfig.properties   最简单的其他的访问数啥的配置都没有加 毕竟是联系配置的 不是做项目

    db.username=root
    db.password=123456
    db.url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8
    db.dirverClass=com.mysql.jdbc.Driver

二.  log4j.properties   这就复制就行  我也不是很明白啥意思

    # This is the configuring for logging displayed in the Application Server
    log4j.rootCategory=DEBUG,stdout,logfile

    #stdout configure
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - <%m>%n

    #logfile configure
    log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/ssm.log
    #log4j.appender.logfile.File=/WEB-INF/logs/ssm.log
    #\u65E5\u5FD7\u6587\u4EF6\u7684\u8DEF\u5F84\u548C\u540D\u79F0
    #log4j.appender.R.File=./../logs/ssm.log
    log4j.appender.logfile.MaxFileSize=10M
    log4j.appender.logfile.MaxBackupIndex=100
    log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
    log4j.appender.logfile.layout.ConversionPattern= %d %p [%c] - <%m>%n

    #debug log for spring
    log4j.logger.org.springframework=DEBUG

    # debug log for ibatis
    log4j.logger.java.sql.Connection=DEBUG
    log4j.logger.java.sql.Statement=DEBUG
    log4j.logger.java.sql.PreparedStatement=DEBUG

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

    <mvc:annotation-driven/>
    <context:component-scan base-package="com.demo" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
      <property name="prefix" value="/WEB-INF/views/"></property>
      <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 下面这段话的意思是我们可以通过${} 来获取classpath:dbconfig.properties中的值 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:dbconfig.properties"/>

    <!-- 这里使用的是阿里巴巴的jar包 包的名字是druid .jar-->
    <bean id="mysqlDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
      <property name="driverClassName"> <value>${db.dirverClass}</value> </property>
      <property name="url" > <value>${db.url}</value> </property>
      <property name="username" > <value>${db.username}</value> </property>
      <property name="password" > <value>${db.password}</value> </property>
    </bean>
    <!-- 这里配置一个jdbcTemplate 用来连接数据库 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <constructor-arg ref="mysqlDataSource" />
    </bean>

    <!-- dao注入 -->
    <bean id="studentDao" class="com.demo.dao.StudentDaoImpl">
      <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
</beans>

7.  这就配置完了 下面是做个测试

1)  首先是页面 login.jsp     通过form做一个提交 这里提交到 controller方法中去--->spring/student/testController

  <form action="spring/student/testController" method="post">
    <br />用户名: <input type="text" name="name"> <br /> <br />年龄: <input
    type="text" name="age"> <br /> <br /> 老师: <input
    type="text" name="teacher">
    <br /> <input type="submit" value="登录">
  </form>

  

2)   StudentController.java    做一个简单的测试 往数据库添加一条数据

  @Controller
  @RequestMapping("/spring/student")
  public class StudentController {
    @Autowired
    private StudentService ss;

    @RequestMapping(value="/testController")
    public String toPage(Student s){
      System.out.println(s.toString());
      s.setId(33);
      ss.save(s);
      return "success";
    }

  }

3)  StudentService  这个没啥注解需要添加

    public interface StudentService {
      public void save(Student student);
    }

4)  StudentServiceImpl

    @Service("StudentService")
    public class StudentServiceImpl implements StudentService {
      @Resource(name="studentDao")
      private StudentDao studentDao;

      public void save(Student student) {
        studentDao.insert(student);
      }

    }

5)StudentDao

    public interface StudentDao {

    public void insert(Student student);

    }

6)  StudentDaoImpl   因为在springmvc中我们给studentDao注入了jdbcTemplate  所以这里可以直接使用     数据库 我就这里就不掩饰了  没事难度

    @Repository("studentDao")
    public class StudentDaoImpl extends JdbcDaoSupport implements StudentDao {

      public void insert(Student student) {
        String sql="insert into users(id,firstname,lastname,phone,email) values(?,?,?,?,?)";
        getJdbcTemplate().update(sql, new Object[]{66,"yijiang","ding","18330698119","email"});
      }

    }

这样我们的maven+springmvc就配置完毕了    说一个容易犯的错误

 

当我们导入的话如果报错      类似于下面 calss类找不到  但是我们ctrl 一点明明有的时候 解决办法:

项目点击右键 点击 Properties 选择Deployment Assembly 再点击右边的Add按钮 选择Java Build Path Entries后点击Next按钮 然后选择你的Maven Dependencies 确定即可

Error configuring application listener of class org.springframework.web.util.Log4jConfigListener

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

maven+springmvc的配置

上一篇:关于在jsp中的路径问题


下一篇:struts2中改变struts.xml默认路径