什么是spring MVC?
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1(现在一般不用),Struts2等。
spring MVC实际上是对JSP/Servlet 进行的封装处理,由框架内的处理器配合各个环节的处理工具,以便控制WEB请求。
Spring MVC架构图:
Spring MVC请求处理流程图:
这次做了个简单的测试:
配置web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<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-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
配置一个springmvc-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
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/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.bdqn.ctr"/>
<mvc:annotation-driven/>
<!-- 完成视图的对应 -->
<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
index.jsp写两个form表单:
<body>
<form action="LoginController/login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="登录">
</form>
<br>
<form action="ControllerTest/login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="登录">
</form>
</body>
/WEB-INF/jsp里新建两个JSP文件(Start.jsp,Show.jsp):
Start.jsp:
<body>
欢迎第${user.uid}位会员,${user.username}
</body>
Show.jsp
<body>
hi,there
</body>
LoginController.java跳转到Start.jsp:
package com.bdqn.ctr;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.bdqn.entity.User;
@org.springframework.stereotype.Controller
@RequestMapping("LoginController")
public class LoginController implements Controller{
@Override
@RequestMapping("/login")
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
String username = request.getParameter("username");
String password = request.getParameter("password");
ModelAndView view = new ModelAndView();
if (username.equals("admin")&&password.equals("123456")) {
User user = new User();
user.setUid(001);
user.setUsername(username);
user.setPassword(password);
view.addObject("user", user);
//跳转到Start.jsp
view.setViewName("Start");
}
return view;
}
}
ControllerTest.java跳转到Show.jsp:
package com.bdqn.ctr;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("ControllerTest")
public class ControllerTest {
@RequestMapping("/login")
public String getMessage(@RequestParam String username,@RequestParam String password){
System.out.println("username:"+username+"\t\t"+"password:"+password);
return "Show";
}
}