Spring MVC屬于SpringFrameWork的后續產品, 已經融合在Spring Web Flow里面 。 Spring 框架提供了構建 Web 應用程序的全功能 MVC 模塊 。 使用 Spring 可插入的 MVC 架構, 從而在使用Spring進行WEB開發時, 可以選擇使用Spring的SpringMVC框架或集成其他MVC開發框架, 如Struts1(現在一般不用), Struts2等 。
工具/原料電腦
SpringMvc
第一種:通過注解的方式進行跳轉1第一種方法:通過HttpServletResponse的API直接輸出
步驟:
1.控制層:controller類的編寫
@Controller
public class RequestController{
@RequestMapping("/request") //映射地址注入
public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {
rp.getWriter().println("request");
}

2 2.web.xml文件的配置
<?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_3_1.xsd"
version="3.1">
<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>

3 3.dispatcher-servlet.xml文件的編寫
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--掃描指定包下所有的注解的類-->
<context:component-scan base-package="com.jsu.mvc"/>
</beans>

4第二種方法:使用HttpServletResponse 類進行重定向跳轉視圖
@RequestMapping("/Response")
public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {
rp.sRedirect(url); //跳轉的指定頁面 。
}
}

5第三種:使用HttpServletRequest 轉發
@RequestMapping("/request")
public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {
req.setAttribute("屬性名","屬性值"); //賦值
req.getRequestDispatcher(url).forward(rq,rp); //跳轉
猜你喜歡
- 中國古代有四大工程指的是哪四大中國古代有四大工程指什么
- Word2003怎么在自動更正列表中添加詞條
- Excel表格中如何求分數的平均值
- Photoshop中制作一個添加按鈕的文字教程
- ”百度hi“APP如何進行中英文模式轉換
- 《西游記》中唐僧、悟空、八戒、沙僧背后的勢力關系大解讀
- 素顏霜一天中什么時候用最好
- keep運動中不讓別人通過通訊錄找到你的方法
- 簡易衣柜安裝方法知多少
- 生活中有哪些被忽略的消防隱患?
