zoukankan      html  css  js  c++  java
  • JAVA-SpringMVC基于注解模式第一个应用

     项目文件结构

    1. 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>test</display-name>
      <!-- 配置springmvc前端配置 -->
      <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
      <param-name>contextConfigLocaltion</param-name>
      <param-value>/WEB-INF/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>
        
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    2.springmvc-servlet.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:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        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-3.1.xsd">
       
       <!-- 配置注解扫描包 -->
        <context:component-scan base-package="com.luo.test"/>
      <!--注册用于支持基于朱姐的控制器的请求处理方法的beans -->
        <mvc:annotation-driven></mvc:annotation-driven>
        <mvc:resources location="/css/" mapping="/css/**"/>
        <mvc:resources location="/" mapping="/*.html"/>
         <!-- 配置前端页面的映射前缀和后缀 -->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <!-- 配置前缀,view的路径 -->
            <property name="prefix" value="/WEB-INF/view/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    </beans>

    3.controller

    package com.luo.test;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloController {
        private static final Log logger=LogFactory.getLog(HelloController.class);
        
        @RequestMapping(value="/hello")
        public String Hello(){
            logger.info("Hello Called");
            return "hello";
        }
    }

    4.view

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <h1>hello</h1>
    </body>
    </html>

    5.运行结果

    注解模式的原理:通过扫描注解文件找到Controller从而找到对应的view 

  • 相关阅读:
    第9.11节 Python中IO模块文件打开读写操作实例
    第9.10节 Python中IO模块其他文件操作属性和方法简介
    Python使用import导入模块时执行了模块的文件但报ModuleNotFoundError错误的愚蠢问题
    Python使用import导入模块时报ValueError: source code string cannot contain null bytes的解决方案
    第9.9节 Python文件随机读写定位操作方法seek
    第9.8节 Python使用writelines函数写入文件内容
    第9.7节 Python使用write函数写入文件内容
    Python中import模块时报SyntaxError: (unicode error)utf-8 codec can not decode 错误的解决办法
    The 2nd tip of DB Query Analyzer
    水仙花数优化问题:穷举法、查找表法、组合数学法
  • 原文地址:https://www.cnblogs.com/dekevin/p/5674124.html
Copyright © 2011-2022 走看看