Loading

controller配置

//声明Spring类的实例是一个控制器
@Controller
//@RequestMapper注解用于映射url到控制器或一个特定的处理程序方法,可用于类和方法
@RequestMapping
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5          version="4.0">
 6     <!--1.配置DispatchServlet-->
 7     <servlet>
 8         <servlet-name>srpingmvc</servlet-name>
 9         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
10         <init-param>
11             <param-name>contextConfigLocation</param-name>
12             <param-value>classpath:springmvc-servlet.xml</param-value>
13         </init-param>
14         <load-on-startup>1</load-on-startup>
15     </servlet>
16     
17     <servlet-mapping>
18         <servlet-name>srpingmvc</servlet-name>
19         <url-pattern>/</url-pattern>
20     </servlet-mapping>
21     
22 </web-app>

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:mvc="http://www.springframework.org/schema/mvc"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7         https://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context
 9         http://www.springframework.org/schema/context/spring-context.xsd
10         http://www.springframework.org/schema/mvc
11         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
12     <!--开启包扫描注解-->
13     <context:component-scan base-package="com.rzk.controller"/>
14 <!--    &lt;!&ndash;替代了  处理器适配器和映射器&ndash;&gt;-->
15 <!--    <mvc:annotation-driven/>-->
16 <!--    &lt;!&ndash;过滤静态资源&ndash;&gt;-->
17 <!--    <mvc:default-servlet-handler/>-->
18 
19    <!--视图解析器 -->
20     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
21           id="InternalResourceViewResolver">
22         <!--前缀-->
23         <property name="prefix" value="/WEB-INF/jsp/"/>
24         <!--后缀-->
25         <property name="suffix" value=".jsp"/>
26     </bean>
27 
28 </beans>

实现多个请求指向一个视图,但页面结果的显示是不一样的,可以实现视图的复用性,而控制器和视图之间弱耦合关系

 1 @Controller
 2 public class ControllerDome2 {
 3     @RequestMapping(path = "/t1")
 4     public String demo1(Model model){
 5 
 6         model.addAttribute("hello","你好1");
 7         /*这里的Login要对应jsp的名字*/
 8         return "Login";
 9     }
10     @RequestMapping(path = "/t2")
11     public String demo2(Model model){
12 
13         model.addAttribute("hello","你好2");
14         /*这里的Login要对应jsp的名字*/
15         return "Login";
16     }
17     @RequestMapping(path = "/t3")
18     public String demo3(Model model){
19 
20         model.addAttribute("hello","你好3");
21         /*这里的Login要对应jsp的名字*/
22         return "Login";
23     }
24 }

Login.jsp页面

<body>
${hello}
</body>

 

 

 

posted @ 2020-04-10 13:52  Rzk  阅读(272)  评论(0编辑  收藏  举报