zoukankan      html  css  js  c++  java
  • java之SpringMVC配置!配置!配置!

    工欲善其事,必先利其器,要想使用SpringMVC自然要先把配置文件写好。

    1.web.xml文件

    新建web项目之后webRoot目录下面会有一个web.xml文件,我们先来对其进行配置。

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

    这里定义了一个servlet,servlet名自己取后面要用到,class固定的,不可改变。

    然后就是初始化init后要加载的xml文件,默认名为servlet名-servlet.xml,这个是要加载到

    webroot-webinf-classes下的,我们将其放到项目目录下的src目录,它会自动加载到classes目录。

    于是我们在src下新建一个xxx-servlet.xml文件

    2.springmvc-servlet.xml

    这里配置上面所提到的servlet名-servlet.xml文件,放在项目目录下的src目录!!!

    <?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: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-4.1.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                    
    
        <!-- spring扫描的包 -->
        <context:component-scan base-package="com.eco.controllor"/>
    
        <!-- DispatcherServlet不处理静态资源,交给服务器默认的servlet处理 -->
        <mvc:default-servlet-handler />
    
        <!-- 启用annotation -->
        <mvc:annotation-driven />
        
        <!-- 视图渲染器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
                id="internalResourceViewResolver">
            <!-- 前缀 -->
            <property name="prefix" value="/WEB-INF/jsp/" />
            <!-- 后缀 -->
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>

    鉴于annotation开发的高效率,这里一致使用annotation。

    上面视图渲染器定义,如果要访问项目名/hello实际上就是访问项目名/WEB-INF/jsp/hello.jsp,这样是符合

    编程开发的“约定优于配置”原则。就像数据库有个表student,那么它的持久化类就应该是Student,我想在

    浏览器访问项目下的hello页面,你就应该帮我指向项目名/WEB-INF/jsp/目录下的hello.jsp页面。

    3.创建JSP页面

    WEB-INF/jsp/目录下创建hello.jsp页面,随便在body中添加几句话。

    4.创建Controller类

    @Controller
    public class Controller {
    
        @RequestMapping("/eco")
        public String hello(){        
            return "hello";
        }
        @RequestMapping("/hi")
        public ModelAndView show(){
            ModelAndView mv=new ModelAndView();
            mv.addObject("msg", "world");
            mv.setViewName("hi");
            return mv;
        }
    }

     类名自定义,下面看注解的意思

    @Controller注解,声明这个类是一个控制器Controller,

    @RequestMapping("/hello") 这个注解当浏览器访问项目名/eco时,跳转到(return)到hello.jsp这个页

    面,也可以是其他页面。下面一个Mapping注解呢,不仅实现了页面跳转(hi.jsp),还设置了一个msg

    变量,可以在hi.jsp中用el表达式来调用它的值;hello ${msg}   输出  hello world。

    5.测试

    接下来在浏览器输入localhost:8080/webmvc/hi

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------

  • 相关阅读:
    openstack计算节点nova
    openstack控制节点nova
    openstack镜像服务glance
    openstack验证服务keystone
    openstack基础环境
    关于mysql中like查询是否通过索引的测试
    并发编程的优缺点
    mybatis中namespace配置方式
    开通技术博客的第一天
    ajax的使用
  • 原文地址:https://www.cnblogs.com/eco-just/p/7873280.html
Copyright © 2011-2022 走看看