首页 > 编程语言 > 详细

SpringMvc 控制器实现的几种方式*

时间:2020-06-01 22:23:33      阅读:62      评论:0      收藏:0      [点我收藏+]

0:springmvc中的配置 有一个特殊的bean配置   ParameterizableViewController 

<!--   专用于jsp 到jsp的转发 不需要经过其他的过滤     就相当于 a.jsp 跳转到 b.jsp --> 
<?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"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
      
        <!--    控制器-
        <bean name="/hello.action" class="com.cn.HelloAction"></bean>
        <bean name="/hello2.action" class="com.cn.HelloAction2"></bean>
        <bean name="/hello3.action" class="com.cn.HelloAction3"></bean>
     -->

<!-- 映射器 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
     -->
<!-- 适配器 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>      --> <!-- 专用于jsp 到jsp的转发 不需要经过其他的过滤 --> <bean name="/index.action" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> <property name="viewName" value="success"></property> </bean> <!-- 视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>

jsp

<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <form action="${pageContext.request.contextPath}/index.action" method="post">
      <input type="submit" value="tijiao"/>
    </form>
  </body>
</html>

 

 

 

 

 

 

1.实现Controller接口 (这种实现可以用 request 和Response来传参 和输入)

public class HelloAction implements Controller {


    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        ModelAndView modelAndView = new ModelAndView();
        System.out.println( httpServletRequest.getParameter("username"));

        modelAndView.addObject("message","helloSpring");
        modelAndView.setViewName("success");  //这样写相对路就需要配置视图解析器

        return modelAndView;
    }
}

 

SpringMvc 控制器实现的几种方式*

原文:https://www.cnblogs.com/gaoSJ/p/13027688.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!