IDEA创建javaWeb

    xiaoxiao2022-07-02  96

    第一部分:新建一个Maven项目 打开IDEA,File>New>Project,选择Maven,选择Project SDK(JDK),勾选Create from archetype(),选择maven-archetype-webapp。

    填写GroupId和ArtifactId

    GroupId和ArtifactId是确定项目在Maven仓库中的坐标,具有唯一性,可以通过他们去查找项目。

    GroupId一般分为多个段,第一段为域,第二段为公司名称。域又分为org、com、cn等等。

    ArtifactId是工程项目名。

    填写Maven home directory,设置settings.xml文件、local repository,在下面的Properties加上archetypeCatalog = internal。

    来自网上的解释:archetypeCatalog表示插件使用的archetype元数据,不加这个参数时默认为remote,local,即中央仓库archetype元数据,由于中央仓库的archetype太多了,所以导致很慢,指定internal来表示仅使用内部元数据。 填写Project name以及项目的物理地址。 选择Enable Auto-Imported. 第二部分:完善项目基本结构。 在main下创建java、resources文件夹,其他文件夹看需求添加。 第三部分:项目配置。 打开File>Project Struction。 project: Modules: 选择项目名字: Sources:

    将java设置为Sources,resources设置为Resources,target设置为Excluded。

    paths:

    设置java源代码编译的目标目录。 Dependencies:

    设置依赖。 Facets: 选择web.xml和webapp文件夹位置: 设置了正确的web.xml以及webapp的位置后,文件上有地球一样的图标显示。

    Artifacts: 第四部分:部署Tomcat: Run>Edit Configurations:

    路径配置: 最后,启动服务器,访问首页,就大功告成了。

    添加springMVC框架,右击项目文件夹Demo,选择Add framework support

    将下图中的Spring和Spring下的Spring MVC都勾上,之前配置pom.xml文件时,已经自动下载了spring相关文件,所以这里就直接用之前下载好的就可以了,OK。(注意:点了Add framework support之后,在下图中有可能会找不到Spring,解决办法在下图的下方) 点击File,选择Project Structure,选择Facets,就会看到有一个Spring啦,右击它,点删除就行啦,然后再回到上面第3步重新Add framework support,Spring就会出现啦。

    现在可以对SpringMVC进行设置了,首先配置web.xml 下图是web.xml自动生成的配置,但是这个版本比较低,我们来换个新的 新配置代码如下,直接复制就好

    <?xml version="1.0" encoding="UTF-8"?> <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_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <!--welcome pages--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--配置springmvc DispatcherServlet--> <servlet> <servlet-name>springMVC</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> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--把applicationContext.xml加入到配置文件中--> <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> </web-app>

    配置dispatcher-servlet.xml,负责mvc的配置

    <?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> <!--此文件负责整个mvc中的配置--> <!--启用spring的一些annotation --> <context:annotation-config/> <!-- 配置注解驱动 可以将request参数与绑定到controller参数上 --> <mvc:annotation-driven/> <!--静态资源映射--> <!--本项目把静态资源放在了webapp的statics目录下,资源映射如下--> <mvc:resources mapping="/css/**" location="/statics/css/"/> <mvc:resources mapping="/js/**" location="/statics/js/"/> <mvc:resources mapping="/image/**" location="/statics/images/"/> <mvc:default-servlet-handler /> <!--这句要加上,要不然可能会访问不到静态资源,具体作用自行百度--> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP--> <!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- --> <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/views/"/><!--设置JSP文件的目录位置--> <property name="suffix" value=".jsp"/> <property name="exposeContextBeansAsAttributes" value="true"/> </bean> <!-- 自动扫描装配 --> <context:component-scan base-package="example.controller"/> </beans>

    配置applicationContext.xml,负责一些非MVC组件的配置,暂时没有所以是空的,但也可以扫描一下

    <?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: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/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="jwj"/> </beans>

    在controller包里新建一个DemoController.java,代码如下:

    package jwj.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/home") public class DemoController { @RequestMapping("/index") public String index(){ return "index"; } }

    项目目录:

    最新回复(0)