zoukankan      html  css  js  c++  java
  • springmvc最简单的搭建,初学者必看

    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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        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">
         
    
        <!-- spring mvc servlet -->
        <servlet>
            <description>spring mvc servlet</description>
            <servlet-name>springMvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <description>spring mvc 配置文件</description>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.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>
    </web-app>

    spring-mvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc" 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"
        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/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
    
        <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
        <context:component-scan base-package="com.dream.controller" />
    
        <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> -->
            <property name="prefix" value="/WEB-INF/content/" />
            <property name="suffix" value=".jsp" />
    
        </bean>
    </beans>

     IndexController.java

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.log4j.Logger;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class IndexController {
    
        private static final Logger logger = Logger.getLogger(IndexController.class);
    
        @RequestMapping(value = "/index2", method = RequestMethod.GET)
        public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
            ModelAndView view = new ModelAndView("manage/login");
            view.addObject("c", "好 ");
            request.setAttribute("a", "hello");
    
            logger.info("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
    
            return view;
        }
    
    }

     login.jsp

    <?xml version="1.0" encoding="UTF-8" ?>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Insert title here</title>
    </head>
    <body>中国人${c}
    </body>
    </html>
  • 相关阅读:
    0-1 RSS订阅
    4-1 文件管理
    3-2 LInux文件管理
    解决docker容器开启端口映射后,会自动在防火墙上打开端口的问题
    samba服务的基本配置
    ftp服务的基本配置
    Linux-Bash终端快捷键
    对systemV和systemd的简单理解(服务方面)
    分析FAT32内部结构-入门篇-
    分享一下今天遇到的两个问题,一个是关于C语言内存泄漏问题,另一个是关于Linux下grep使用时的问题
  • 原文地址:https://www.cnblogs.com/happyday56/p/4526584.html
Copyright © 2011-2022 走看看