新建一个项目:然后点击maven,勾选Greate from archetype选择下面的红圈
然后next:
接着:第一个随便填,最好是有调理。第二个就是项目名,第三个就是版本号。
接着next,然后就是这样的,就有你的maven和它的配置的位置。我好像是之前配置了,所以是下面的这个界面。如果不是,那么就在idea的开始界面配置maven的位置。网上有很多。或者看我上一篇。
一路next,然后finsh。
第一次可能要等一下,因为maven在下载jar。最后的结构就是下面那样:
这样,还不算好,因为没有将spring和springmvc的依赖加进去
在
然后配置web.xml文件:
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">
然后配置spring-mvc.xml文件:
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">
最后配置applicationContext.xml。
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">
新建一个jsp文件夹,然后在里面添加jsp文件。
继续新建一个com.controller文件。新建一个类,
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class springcontroller {
@RequestMapping("stringfirst")
public String first() {
System.out.println("成功");
return "zhujietest";
}
}
看看结构目录
然后,启动idea,
接着在网址后面输入:stringfirst
大概的流程说一下吧。我也不知道对不对,不对请纠正。
程序,首先初始化web.xml,找到
接着在网址输入@RequestMapping("stringfirst"),包裹的路径,程序通过这个路径更准确的说是请求,找到控制器,拦截,判断是不是符合规则。符合就找到注解的controller,然后通过return,转到视图解析器,找到jsp文件。于是返回到了stringfirst.jsp页面。:
这就是大概的流程。为什么说springmvc,要加一个mvc呢,其实是因为,它帮我们做好了mvc的架子。它内部封装好了,m(model)v(view),我们只需要写c(Controller)的代码,其他的只需要交给springmvc来反射就好了。当然,界面的你自己写。