SpringMVC是一个服务端MVC框架,用于接收游览器请求,进行数据处理并做出响应。这里简单地使用SpringMVC写一个小例子。
- 简单使用
这里使用的工具是idea,目前最热门的java开发工具。首先用idea创建一个SpringMVC项目,它会帮你自动下载需要的包。
因为是web项目,然后需要配置Tomcat服务。在Add configuration中配置Tomcat server
还得配置一个Artifacts。
然后你可以修改Application context 的名称
最后一步,在project structure中的artifacts部分导入springmvc的包
现在。可以开始写代码了。我们创建项目后,idea就以及帮我们写好了大部分配置,我们只需要再去添加修改就可以了。在web.xml中,已经帮我们配置好了DispatcherServlet,我们只需要按照我们所需要的过滤规则设置url过滤就行了,在url-pattern处修改,我这里是修改成了让它springMVC拦截.form结尾的请求。如果改成/,则是拦截所有请求
<?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>*.form</url-pattern>
</servlet-mapping>
</web-app>
dispatcher-servlet.xml SpringMVC的核心配置文件,在这里加入我们的各种配置。
<?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">
<!--指定扫描@controller注解的范围为controller包下-->
<context:component-scan base-package="controller"/>
<!--让SpringMVc支持@controller注解-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--视图页面的前缀 也就是返回jsp文件夹下的视图-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--视图页面的后缀 也就是是返回jsp视图-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
再来看看我们的controller包下的TestController类
@Controller
public class TestController {
@RequestMapping("/test")
public String Test(){
System.out.println("hello test");
return "test";
}
}
这个controller的意思是如果接受的test.form请求(因为配置了.form后缀),就返回WEB-INF/jsp文件夹下的test.jsp页面,@RequestMapping这个注解用来将请求映射到controller中。
如上例子,在游览器输入 localhost:8080/SpringMvcTest/test.form 就会跳转的相应的jsp页面。
我使用springMVC主要是更Android交互,所以需要返回的是一个json字符串而不是一个页面,那么我们的controller可以这样写。加了@ResponseBody后返回的就是一个数据体而不是页面了
@Controller
public class TestController {
@RequestMapping("/test")
public @ResponseBody String Test(){
System.out.println("hello test");
return "test";
}
}
我暂时只用到这么些内容,就介绍到这。