新建一个工程
-
点击Create New Project
-
选择SpringMVC和Web Application,然后我们因为没有下载SpringMVC的包,因此选择Download进行下载
- 给项目取名字后点击Finish完成创建
对项目结构进行完善
-
新建一个lib和classes目录
在WEB-INF下新建这两个目录
其中lib来存放需要添加的jar包。
-
在Project Structure中点击Modules修改如下东西
并把我们刚刚的lib添加到:

然后在Artifacts中进行Fix。
-
配置Tomcat
添加一个Tomcat
这一步可有可无
这样既可完成对项目的部署了。
编写HelloWorld
- 我们新建项目后打开
web.xml
,里面已经给我们自动生成了代码了,我们需要对代码进行改动
对于<servlet-mapping>
节点中的<url-pattern>
进行修改,改为<url-pattern>/</url-pattern>
,更改后的代码如下:
<?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_4_0.xsd"
version="4.0">
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
-
xxx-servlet.xml
默认配置文件
这个默认配置文件的xxx取决于上面web.xml
中配置的servlet
的名字,与其一致,并且该文件在WEB-INF目录下,因此我们的配置文件名为: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: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">
<!-- 配置自动扫描的包-->
<context:component-scan base-package="com.cerr.springmvc.handlers"/>
<!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver" >
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/views/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 修改index.jsp
给其添加一个超链接。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="helloworld">HelloWorld</a>
</body>
</html>
- 新建一个Controller类
使用@RequestMapping来映射请求的url
返回值会通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver视图解析器,会做如下的解析:
通过prefix+returnVal+后缀 的方式得到实际的物理视图,然后做转发操作
新建一个HelloWorld,并给其注解@Controller
,给其方法注解上@RequestMapping()
,里面填入要映射请求的url,例如前面<a href="helloworld">HelloWorld</a>
,则在此处注解为:@RequestMapping("/helloworld")
。对于我们的配置:得到的是/WEB-INF/views/+success +.jsp 即 /WEB-INF/views/success.jsp,因此我们在WEB-INF目录下新建一个views目录,然后新建一个success.jsp文件。
package com.cerr.springmvc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
@RequestMapping("/helloworld")
public String hello(){
System.out.println("hello world");
return "success";
}
}
- 转发目的jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h4>hhh</h4>
</body>
</html>
这样helloworld即编写好了,直接运行就行。
网友评论