• idea+maven+springmvc+helloworld


    1、添加依赖,并在项目上添加Spring mvc框架的支持(add FrameWork Support):

     1 <dependency>
     2       <groupId>junit</groupId>
     3       <artifactId>junit</artifactId>
     4       <version>4.12</version>
     5       <scope>test</scope>
     6     </dependency>
     7     <!--日志-->
     8     <dependency>
     9       <groupId>org.slf4j</groupId>
    10       <artifactId>slf4j-log4j12</artifactId>
    11       <version>1.7.21</version>
    12     </dependency>
    13     <!--J2EE-->
    14     <dependency>
    15       <groupId>javax.servlet</groupId>
    16       <artifactId>javax.servlet-api</artifactId>
    17       <version>3.1.0</version>
    18     </dependency>
    19     <dependency>
    20       <groupId>javax.servlet.jsp</groupId>
    21       <artifactId>jsp-api</artifactId>
    22       <version>2.2</version>
    23     </dependency>
    24     <dependency>
    25       <groupId>javax.servlet</groupId>
    26       <artifactId>jstl</artifactId>
    27       <version>1.2</version>
    28     </dependency>
    29     <!--mysql驱动包-->
    30     <dependency>
    31       <groupId>mysql</groupId>
    32       <artifactId>mysql-connector-java</artifactId>
    33       <version>5.1.35</version>
    34     </dependency>
    35     <!--springframework-->
    36     <dependency>
    37       <groupId>org.springframework</groupId>
    38       <artifactId>spring-web</artifactId>
    39       <version>4.2.6.RELEASE</version>
    40     </dependency>
    41     <dependency>
    42       <groupId>org.springframework</groupId>
    43       <artifactId>spring-webmvc</artifactId>
    44       <version>4.2.6.RELEASE</version>
    45     </dependency>
    46     <dependency>
    47       <groupId>org.springframework</groupId>
    48       <artifactId>spring-context</artifactId>
    49       <version>4.2.6.RELEASE</version>
    50     </dependency>
    51     <dependency>
    52       <groupId>org.springframework</groupId>
    53       <artifactId>spring-test</artifactId>
    54       <version>4.2.6.RELEASE</version>
    55     </dependency>
    56       <dependency>
    57           <groupId>org.springframework</groupId>
    58           <artifactId>spring-jdbc</artifactId>
    59           <version>4.2.6.RELEASE</version>
    60       </dependency>
    61     <dependency>
    62       <groupId>com.github.stefanbirkner</groupId>
    63       <artifactId>system-rules</artifactId>
    64       <version>1.16.1</version>
    65       <scope>test</scope>
    66     </dependency>
    67     <dependency>
    68       <groupId>org.aspectj</groupId>
    69       <artifactId>aspectjweaver</artifactId>
    70       <version>1.8.9</version>
    71     </dependency>
    72     <!--其他需要的包-->
    73     <dependency>
    74         <groupId>org.apache.commons</groupId>
    75         <artifactId>commons-lang3</artifactId>
    76         <version>3.4</version>
    77     </dependency>
    78     <dependency>
    79         <groupId>commons-fileupload</groupId>
    80         <artifactId>commons-fileupload</artifactId>
    81         <version>1.3.1</version>
    82     </dependency>

    2、配置web.xml 

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     5          version="3.1">
     6 
     7     <display-name>Archetype Created Web Application</display-name>
     8 
     9     <!--welcome pages-->
    10     <welcome-file-list>
    11         <welcome-file>index.jsp</welcome-file>
    12     </welcome-file-list>
    13 
    14     <!--配置springmvc DispatcherServlet-->
    15     <servlet>
    16         <servlet-name>springMVC</servlet-name>
    17         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    18         <init-param>
    19             <!--配置dispatcher.xml作为mvc的配置文件-->
    20             <param-name>contextConfigLocation</param-name>
    21             <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    22         </init-param>
    23         <load-on-startup>1</load-on-startup>
    24         <async-supported>true</async-supported>
    25     </servlet>
    26     <servlet-mapping>
    27         <servlet-name>springMVC</servlet-name>
    28         <url-pattern>/</url-pattern>
    29     </servlet-mapping>
    30     <!--把applicationContext.xml加入到配置文件中-->
    31     <context-param>
    32         <param-name>contextConfigLocation</param-name>
    33         <param-value>/WEB-INF/applicationContext.xml</param-value>
    34     </context-param>
    35     <listener>
    36         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    37     </listener>
    38 </web-app>

    3、配置dispatcher-servlet.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xmlns:mvc="http://www.springframework.org/schema/mvc"
     6        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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
     7     <!--此文件负责整个mvc中的配置-->
     8 
     9     <!--启用spring的一些annotation -->
    10     <context:annotation-config/>
    11 
    12     <!-- 配置注解驱动 可以将request参数与绑定到controller参数上 -->
    13     <mvc:annotation-driven/>
    14 
    15     <!--静态资源映射-->
    16     <!--本项目把静态资源放在了webapp的statics目录下,资源映射如下-->
    17     <mvc:resources mapping="/css/**" location="/WEB-INF/statics/css/"/>
    18     <mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/"/>
    19     <mvc:resources mapping="/image/**" location="/WEB-INF/statics/image/"/>
    20 
    21     <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
    22     <!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
    23     <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    24         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    25         <property name="prefix" value="/WEB-INF/views/"/><!--设置JSP文件的目录位置-->
    26         <property name="suffix" value=".jsp"/>
    27         <property name="exposeContextBeansAsAttributes" value="true"/>
    28     </bean>
    29 
    30     <!-- 自动扫描装配 -->
    31     <context:component-scan base-package="example.controller"/>
    32 </beans>

    4、配置applicationContext.xml 

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <beans xmlns="http://www.springframework.org/schema/beans"
    3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4        xmlns:context="http://www.springframework.org/schema/context"
    5        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.xsd">
    6     <context:component-scan base-package="example"/>
    7 </beans>

    5、编写controller

    1 @Controller
    2 @RequestMapping("/home")
    3 public class IndexController {
    4 
    5     @RequestMapping("/index")
    6     public String index() {
    7         return "index";
    8     }
    9 }

    6、在WEB-INF下建立views文件夹,并在这个文件夹下建立文件test.jsp

    7、运行调试;

    另外集成mybatis可以参考文章:https://www.cnblogs.com/xiaoL/p/7753130.html

  • 相关阅读:
    自定义提示框(alert、confirm 可自定义标题 内容 图标 取消按钮)
    Node.js 技術
    ASP.NET MVC+Spring.net+Nhibernate+EasyUI+Jquery开发案例(1)
    Memcached 命令简介
    MySQL 请选择合适的列
    WCF光芒下的Web Service
    如何实现共享软件网络授权认证,包括注册新用户、登录、修改密码等操作
    SOA面向服务架构简述
    Python Web 框架, 使用 Django 和 Python 开发 Web 站点
    Interface继承至System.Object?
  • 原文地址:https://www.cnblogs.com/laoxia/p/9311442.html
Copyright © 2020-2023  润新知