暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

基于Spring使用Idea搭建简化版Web应用

码蚁在线 2019-09-30
65


引言

本来打算到网上找一个Demo示例直接使用,发现很多各种版本,各种配置的都有,而不能精简,因此,为了能够快速搭建Spring的Web简版开发环境,并能够了解其中的配置,将整个过程记录下来,也为后续自己需要的时候可以直接使用。

前提条件

开发工具:Idea 使用框架:maven+spring

基于Maven搭建Web项目

1、使用Idea的Maven创建项目,选择maven-archetype-webapp2、填写groupId和artifactId3、点击next后,如图4、一直next到finish后就会生成一个web的项目,配置tomcat运行其中未对生成后的目录文件进行讲解,因为比较简单

5、启动完成后,就会弹出网页此时的Web项目就创建完成,我们继续添加Spring相关的组件来实现从前端到服务端的请求。

Spring+SpringMVC实现请求

借用idea的工具,直接在Project项目上右键,选择“Add Framework Support...”选择SpringMVC后点击“OK” 直接会生成需要的配置文件,如图:

配置文件

现在开始对配置文件进行最简化的配置,将项目能够运行起来 1、web.xml

  1. <!DOCTYPE web-app PUBLIC

  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >


  4. <web-app>

  5. <display-name>Spring WebProject</display-name>

  6. <context-param>

  7. <param-name>contextConfigLocation</param-name>

  8. <param-value>/WEB-INF/applicationContext.xml</param-value>

  9. </context-param>

  10. <listener>

  11. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  12. </listener>


  13. <servlet>

  14. <servlet-name>dispatcher</servlet-name>

  15. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  16. <init-param>

  17. <param-name>contextConfigLocation</param-name>

  18. <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>

  19. </init-param>

  20. <load-on-startup>1</load-on-startup>

  21. </servlet>

  22. <servlet-mapping>

  23. <servlet-name>dispatcher</servlet-name>

  24. <url-pattern>/</url-pattern>

  25. </servlet-mapping>

  26. </web-app>

复制

2、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 https://www.springframework.org/schema/context/spring-context.xsd">


  6. </beans>

复制

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. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">


  6. <context:component-scan base-package="com.chenyanwu">

  7. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />

  8. </context:component-scan>

  9. </beans>

复制

其他代码

1、pom文件中需要添加的配置

  1. <dependency>

  2. <groupId>org.springframework</groupId>

  3. <artifactId>spring-webmvc</artifactId>

  4. <version>5.1.8.RELEASE</version>

  5. </dependency>

  6. <dependency>

  7. <groupId>javax.servlet</groupId>

  8. <artifactId>javax.servlet-api</artifactId>

  9. <version>4.0.1</version>

  10. <scope>provided</scope>

  11. </dependency>

复制

2、对应的Controller类

  1. /**

  2. * @Auther: chenyanwu

  3. * @Date: 2019/9/30 16:38

  4. * @Description:

  5. * @Version 1.0

  6. */

  7. @Controller

  8. @RequestMapping("/hello")

  9. public class HelloController {


  10. @RequestMapping("/say")

  11. @ResponseBody

  12. public String sayHello(String name) {

  13. return "Hello, " + name;

  14. }

  15. }

复制

到此代码对应的配置和代码已经写完,启动服务后:浏览器中输入:http://localhost:8080/hello/say?name=chenyanwu 结果:对于配置文件:web.xml、applicationContext.xml、dispatcher-servlet.xml的文件详细说明,后续在抽时间详细罗列讲解!

更多精彩,更多技术请关注:码蚁在线(coding_online)

文章转载自码蚁在线,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论