概述
使用idea编译器来搭建springMVC,新手小白初次搭建,写的不好请大家多多指教,谢谢~
(1)用idea来创建一个maven项目先
生成maven项目如下
(2)添加spring所需jar
<!--spring依赖jar--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.14.RELEASE</version> </dependency>(3)在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:/spring/applicationContext.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>(4)spring配置文件中开启注解扫描以及视图解析器
<mvc:annotation-driven/> <!--注解扫描,不会扫描@Service/@Repository的Bean--> <context:component-scan base-package="com.test.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--视图解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!--前缀--> <property name="prefix"> <value>/WEB-INF/test/</value> </property> <!--后缀--> <property name="suffix"> <value>.jsp</value> </property> </bean>(5)创建controller和jsp页面
package com.test.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by hp on 2018/4/9. */ @Controller @RequestMapping(value = "/index") public class WelcomeController { @RequestMapping(value = "/toWelcome") public ModelAndView toWelcome(HttpServletRequest request, HttpServletResponse response) { ModelAndView mv = new ModelAndView(); mv.addObject("message", "Hello, Welcome to SpringMVC!"); mv.setViewName("welcome"); return mv; } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>SpringMVC DEMO</title> </head> <body> <h1>This is SpringMVC Demo</h1> ${requestScope.message} </body> </html>(6)idea中配置tomcat
(7)运行项目
简单的springMVC通过idea就搭建成功了。
最后
以上就是鲜艳美女为你收集整理的IDEA+MAVEN搭建简单的springMVC的全部内容,希望文章能够帮你解决IDEA+MAVEN搭建简单的springMVC所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复