在IDEA下配置一个Springmvc项目
一、创建一个空项目1.File->New Project->Maven点击Next2.这里的GroupId和ArtifactId在学习时随便填(说明一下:groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。 groupId一般分为多个段,这里我只说两段,第一段为域,第
一、创建一个空项目
1.File->New Project->Maven
点击Next
2.这里的GroupId和ArtifactId在学习时随便填
(说明一下:groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。
groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。
比如我创建一个项目,我一般会将groupId设置为cn.zr,cn表示域为中国,zr是我个人姓名缩写,artifactId设置为testProj,表示你这个项目的名称是testPr)
点击Next
3.在这里要解决maven加载项目过慢问题:
通过上面操作你会发现嗖的一下就好了,下面看到只用了几秒钟
4.新建好的项目结构
其中java,resources和test可能需要自己手动创建。需要自己创建目录并右击创建的目录选择Mark Directory as为相应的项目结构
5.右击项目,选择第二个Add Framework Support,按下图打勾,然后点击🆗
然后你会发现项目结构有点小变化
二、配置xml文件
1.web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--配置dispatcher.xml作为mvc的配置文件-->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.dispatcher-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--启用spring的一些annotation -->
<context:annotation-config/>
<!-- 自动扫描装配 -->
<context:component-scan base-package="com.controller"/>
<mvc:default-servlet-handler />
<!-- 配置注解驱动 可以将request参数与绑定到controller参数上 -->
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/views/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
3.在java路径下创建一个com.controller包,在该包下创建一个HelloController.java(包名和类名随意起,但是包名要在dispatcher-servlet.xml配置中自动扫描包对应)
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class helloSpring {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
4.在WEB-INF下新建一个views目录,并在该目录下新建一个hello.jsp。这里我们只让输出一段文字
<body>
hello,Springmvc!
</body>
三、将项目运行在tomcat服务器上
点击左上角“+”号,按照下图选择
选择Deployment
选择第一个,将war包部署到服务器上
点击🆗后,出现以下界面
将Application contexts周免文本框中的东西删掉(只是让地址栏中URl显得好看,不删也没关系。)
更多推荐
所有评论(0)