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.sendRedirect(url); //跳转的指定页面。
}
}
- 5
第三种:使用HttpServletRequest 转发
@RequestMapping("/request")
public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {
req.setAttribute("属性名","属性值"); //赋值
req.getRequestDispatcher(url).forward(rq,rp); //跳转
}
第二种:使用Springvc提供的Model类
- 1
第一种:使用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>
- 2
第二种:使用modelview,它不需要配置视图解析器,但是不能指定跳转页面
1.控制层controller的编写
@RequestMapping("/modelmap")
public String modelHello(String A,ModelMap B){
map.addAttribute("A",B); // 通过ModelMap键值对的方式设置传值
System.out.println(B);
return "url";
}
注意事项
- 希望对读者有所帮助~~~喜欢的请给小编点个大拇指
来源:百闻(微信/QQ号:9397569),转载请保留出处和链接!
本文链接:https://www.ibaiwen.com/web/246561.html
- 上一篇: wps文字/表格护眼和夜间模式如何开启或关闭
- 下一篇: python基础语法--算数运算
- 热门文章
-
WB蒙特利尔(WB Montreal)——欧美十大最差视频游戏开发商
迅猛龙(Velociraptor)——欧美史前十大死亡动物
什么是果酱猫(What Marmalade Cats)?
神奇蜘蛛侠2(The Amazing Spider-Man 2)——欧美最佳蜘蛛侠电影
希瑟(Heather)——欧美十大最佳柯南灰歌
二人梭哈
faceu激萌怎么把瘦脸开到最大
奥兹奥斯本(Ozzy Osbourne)——欧美十大高估歌手
什么是小脑前下动脉(Anterior Inferior Cerebellar Artery)?
我应该知道康涅狄格州的什么(What Should I Know About Connecticut)?
- 热评文章
- 最新评论
-
- 最近访客
-
- 站点信息
-
- 文章总数:200248
- 页面总数:9
- 分类总数:1
- 标签总数:0
- 评论总数:0
- 浏览总数:497