zoukankan      html  css  js  c++  java
  • Java Spring MVC框架搭建(一)

    环境准备 >>>>>>java JDK和tomcat,eclipse

    1.创建项目

    2.项目名称自定义,这边为demo

    3.我们已经创建完一个动态网站的项目,还得下载Spring MVC相关的jar包,Spring MVC有很多版本,下面提供的是3.2.8的版本,如果需要其他不同版本,自行网上搜索下载,或者到官网

    http://projects.spring.io/spring-framework/

    http://repo.springsource.org/libs-release-local/org/springframework/spring/3.2.8.RELEASE/spring-framework-3.2.8.RELEASE-dist.zip

    下载解压

    将里面的jar包,粘贴到 >>>>>>  E:eclipse-workplacedemoWebContentWEB-INFlib这是我项目路径

    commons-loging的http://mirrors.tuna.tsinghua.edu.cn/apache//commons/logging/binaries/commons-logging-1.2-bin.zip用来记录程序运行的日志活动。下载完,将commons-loggin-1.2.jar和commons-logging-1.2-javadoc.jar复制到项目的lib文件夹下。

    4.还需要对项目配置server

    5.然后配置站点文件web.xml

    web.xml是用来初始化配置信息的,如welcome页面,listener等

    配置web.xml信息

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        <description>
          Spring MVC DEOM
        </description>
        <display-name>springMVC</display-name>
        
        <!-- 
            CharacterEncodingFilter类具有encoding和forceEncoding两个属性,其中encoding是表示设置request的编码,forceEncoding表示是否同时设置response的编码。 
            <filter>下的<filter-name>内的值和<filter-mapping>下的<filter-name>内的值要完全一致
        -->
        <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>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        
        <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <!-- /下的所有请求都为UTF-8编码 -->
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        
        <!-- 
            Spring MVC相关设置
            <servlet>下的<servlet-name>内的值和<servlet-mapping>下的<servlet-name>内的值要完全一致
        -->
        <servlet>
            <servlet-name>SpringMVC-DEOM</servlet-name>
            <!-- DispatcherServlet主要负责流程的控制 -->
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- Spring MVC配置文件路径 -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/applicationContext.xml</param-value>
            </init-param>
            <!-- 表示启动容器时初始化该Servlet -->
            <load-on-startup>1</load-on-startup>
        </servlet>
        
        <servlet-mapping>
            <servlet-name>SpringMVC-DEOM</servlet-name>
            <!-- 表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。 -->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        
    </web-app>
    
     6.Spring MVC配置文件applicationContext.xml,就是spring的配置信息,要实现什么都是通过配置文件来实现的  
    
    <?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.2.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-4.2.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.2.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
      
      <bean id = "helloworld" class="com.test.controller.HelloWorld">
      </bean>
    </beans>
            
    </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: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.2.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-4.2.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.2.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
      
      <bean id = "helloworld" class="com.test.controller.HelloWorld">
      </bean>
    </beans>

    id:确定该Bean的唯一标识符,容器对Bean管理、访问、以及该Bean的依赖关系,都通过该属性完成。Bean的id属性在Spring 配置文件中是唯一的。    

     class:指定该Bean的具体实现类。注意这里不能使接口。通常情况下,Spring会直接使用new关键字创建该Bean的实例,因此,这里必须提供Bean实现类的类名。

     6.Java Resource -> src上 右键点击-> new ->package  建立com.test.controller 建立一个包,之后在这个包上右键点击new->class ,类名为HelloWorld,之后点击Finish.

    7.HelloWorld.java代码为

    7.然后测试访问

  • 相关阅读:
    GIt-重置
    Git-对象
    Git-暂存区
    Git-Git初始化
    Git-起步
    调试九法-制造失败
    调试九法-理解系统
    readhat7.0 bond配置
    firewall-cmd 防火墙命令详解 及 TCP Wrappers
    RAID与LVM磁盘阵列技术
  • 原文地址:https://www.cnblogs.com/dslx/p/10157143.html
Copyright © 2011-2022 走看看