springmvc框架得搭建大致过程如下:
springmvc所依赖的jar包传送门(点击下载)
DispatcherServlet配置工具传送门(点击下载)
当然你可以去springmvc的官网下载
其中web.xml文件的基础配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- The front controller of this Spring Web application, responsible for
handling all application requests -->
<!-- DispatcherServlet的配置用插件来完成。 注意事项: 1,参数初始化:告知当前mvc的配置文件路径 <init-param>
<param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value>
</init-param> 2,配配置当前servlet的映射,url-pattern的参数修改为:/ (所有的请求都要经过servlet) -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
过滤器什么的配置小编后续会跟上
springmvc.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">
<!-- 配置 @controller @service -->
<!-- *表示让所有的包都识别 -->
<context:component-scan base-package="com.springmvc.handler"></context:component-scan>
<!-- 配置视图解析器 :逻辑视图、物理视图(***.jsp) 视图解析器会将逻辑视图自动转换为物理视图 比如物理视图为hello, 转化为:hello.jsp -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<beans>
在请求页面写一行代码:
<!-- 访问路径:类注解/方法注解 -->
<a href="springmvc/hello?name=tom&age=13">hello world</a>
新建一个普通的java类,并且标注为控制器:
@Controller
@RequestMapping("/springmvc")
public class HelloWorldHandler {
/**
* 控制其请求业务方法的格式: public string hello(){
*
* } String:当前方法处理完毕后,要返回的逻辑视图名
*/
// 对hello这个控制器的方法进行映射
// method...:约定请求方式为post
/**
* 参数说明:method:请求方式; params:请求参数 headers:请求头
*/
@RequestMapping(value = "/hello", method = RequestMethod.GET, params = { "name=tom", "age!=12" }, headers = {
"accept=text/html" })
public String hello() {
System.out.println("运行到这里了。。。。。。");
return "success";// 找到一个叫success.jsp的页面
}
}
第一个springmvc程序的实现过程大致就这样了,我们看看效果: