Spring MVC屬于SpringFrameWork的后續產品,已經融合在Spring Web Flow里面。Spring 框架提供了構建 Web 應用程序的全功能 MVC 模塊。使用 Spring 可插入的 MVC 架構,從而在使用Spring進行WEB開發時,可以選擇使用Spring的SpringMVC框架或集成其他MVC開發框架,如Struts1(現在一般不用),Struts2等。
第一種方法:通過HttpServletResponse的API直接輸出
步驟:
1.控制層:controller類的編寫
@Controller
public class RequestController{
@RequestMapping("/request") //映射地址注入
public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {
rp.getWriter().println("request");
}
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.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>
第二種方法:使用HttpServletResponse 類進行重定向跳轉視圖
@RequestMapping("/Response")
public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {
rp.sendRedirect(url); //跳轉的指定頁面。
}
}
第三種:使用HttpServletRequest 轉發
@RequestMapping("/request")
public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {
req.setAttribute("屬性名","屬性值"); //賦值
req.getRequestDispatcher(url).forward(rq,rp); //跳轉
}
第一種:使用modelandview進行跳轉。但是需要配置視圖解析器,而且能指定跳轉頁面。
1.控制層controller的編寫
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest,
javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
ModelAndView view = new ModelAndView();
view.addObject("A","B"); //A為屬性名,B為屬性值
view.setViewName("index"); // 指定視圖的名稱
return view;
}
}
2.SpringMvc-servlet.xml的配置
<!--配置渲染器-->
<!--配置controller中頁面的位置-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!--視圖的前綴-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--視圖的后綴-->
<property name="suffix" value=".jsp"/> //指定跳轉的頁面為controller類設置的視圖名后面加上.jsp
</bean>
<bean name="/index.do" class="com.jsu.mvc.HelloController"></bean>
第二種:使用modelview,它不需要配置視圖解析器,但是不能指定跳轉頁面
1.控制層controller的編寫
@RequestMapping("/modelmap")
public String modelHello(String A,ModelMap B){
map.addAttribute("A",B); // 通過ModelMap鍵值對的方式設置傳值
System.out.println(B);
return "url";
}
0 篇文章
如果覺得我的文章對您有用,請隨意打賞。你的支持將鼓勵我繼續創作!