Spring入门学习(五、SpringMVC入门)
示意图
- 我们可以很清楚的看出这个SpringMvc其实就是一个大型的Servlet,可以接受无数的请求,只是和一般的有些不一样
- 我们一般都是一个请求转发给一个servlet,然而MVC却是接受很多请求,然后将这些请求转发给其余的JAVA类处理
- 最后将结果返回给响应
- 因为MVC的本质是一个Servlet,所以我们需要Web.xml配置,然而路径这个时候我们就可以写“/*”
入门案例
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">
<display-name>SpringMVC_1</display-name>
<servlet>
<servlet-name>SpringMVC</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>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
- 这里就是一个基础的servlet配置
- 这里的class路径对应的是我们的jar包中的一个文件
- 因为SpringMVC需要管理其余的java类,所以相当于一个Spring,所以我们需要配置一个参数,引入Spring的配置文件
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
- 因为我们需要使用注解,所以前面一样的是注解需要的头部,复制粘贴即可
-
<mvc:annotation-driven></mvc:annotation-driven>
代表开启注解 -
<context:component-scan base-package="controller"></context:component-scan>
表示以扫描包的形式配置bean -
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
这个属性后面讲解
Login.jsp
<a href="springmvc.action">帅帅哒</a>
Result.jsp
空
LoginController
@Controller
public class LoginController {
@RequestMapping(value="/springmvc.action",method= {RequestMethod.POST})
public String text01() {
System.out.println("SpringMvc 帅帅哒");
return "result";
}
}
-
这个就是我们的普通java类了
-
我们通过 @Controller配置为bean
-
@RequestMapping(value="/springmvc.action",method= {RequestMethod.POST})
这个注解就是我们SpringMvc的映射了,我们login.jsp的"a"标签就是通过这个访问的我们这个servlet -
然后注意我们的返回,我们只返回了“result”
-
但是我们注意刚刚Spring的配置文件
<property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property>
-
配置了这两句话,这两句话就是代表,将“result”,配置成“/result.jsp”
-
这样就是一个最基础的SPringMvc的入门案例了
总结:
- 我们MVC主要就是就是@RequestMapping这个注解,这个注解是映射url的关键
- 其中@RequestMapping有两个特性
- 窄化请求
- 限制http请求方法
窄化请求
我们在类名上加了@RequestMapping,就代表要访问这个类就必须要加上“User”才可以,例如我们的访问方法就必须写成<a href="User/springmvc.action">帅帅哒</a>
限制http请求方法
我们刚刚已经看到了这个方法,就是在后面加一个method=“XXX”就搞定,这样,如果是“a”标签,或者表单默认提交的话,就无法访问,因为“a”标签以及表单的默认都是“GET”方法而不是“POST”的方法
以上就是 @RequestMapping的两个特性