SpringMVC开发中两种页面跳转方式( 二 )


    }

SpringMVC开发中两种页面跳转方式



第二种:使用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=https://vvvtt.com/article/"org.springframework.web.servlet.view.JstlView"/>
  <!--视图的前缀-->
  <property name="prefix" value=https://vvvtt.com/article/"/WEB-INF/jsp/"/>
  <!--视图的后缀-->
  <property name="suffix" value=https://vvvtt.com/article/".jsp"/> //指定跳转的页面为controller类设置的视图名后面加上.jsp
</bean>
  <bean name="/index.do" class="com.jsu.mvc.HelloController"></bean>

SpringMVC开发中两种页面跳转方式



2第二种:使用modelview, 它不需要配置视图解析器, 但是不能指定跳转页面
1.控制层controller的编写
 @RequestMapping("/modelmap")
 public String modelHello(String A,ModelMap B){
   map.addAttribute("A",B);   // 通过ModelMap键值对的方式设置传值
   System.out.println(B);
   return "url";
【SpringMVC开发中两种页面跳转方式】 }

SpringMVC开发中两种页面跳转方式



注意事项希望对读者有所帮助~~~喜欢的请给小编点个大拇指

以上内容就是SpringMVC开发中两种页面跳转方式的内容啦, 希望对你有所帮助哦!

猜你喜欢