Loading

springmvc使用注解

 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 
 7     <!--配置DispatchServlet 这个是SpringMVC的核心 :请求分发器,前端控制器-->
 8     <servlet>
 9         <servlet-name>dispacthServlet</servlet-name>
10         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
11 
12         <!--DispatcherServlet要绑定Spring的配置文件-->
13         <init-param>
14             <param-name>contextConfigLocation</param-name>
15             <param-value>classpath:springmvc-servlet.xml</param-value>
16         </init-param>
17         <!--启动级别-->
18         <load-on-startup>1</load-on-startup>
19     </servlet>
20 
21     <!--
22         在SpringMVC中  :/  和  /*
23         /   :  只匹配所有的请求,不会去匹配jsp页面
24         /*  :   匹配所有的请求,会去匹配jsp页面
25     -->
26     <servlet-mapping>
27         <servlet-name>dispacthServlet</servlet-name>
28         <url-pattern>/</url-pattern>
29     </servlet-mapping>
30 
31 </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     <!--自动扫描包  让指定包下的注解生效   由IOC容器统一管理-->
13     <context:component-scan base-package="com.rzk.controller"/>
14     <!--让Spring不处理静态资源 过滤-->
15     <mvc:default-servlet-handler/>
16 
17     <mvc:annotation-driven/>
18 
19 
20     <!--视图解析器:DispatcherServlet给他的ModelAndView-->
21     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
22           id="InternalResourceViewResolver">
23         <!--前缀-->
24         <property name="prefix" value="/"/>
25         <!--后缀-->
26         <property name="suffix" value=".jsp"/>
27     </bean>
28 
29 </beans>

 

 1 //代表这个类被Spring接管,被这个注解的类中所有的方法,
 2 // 如果返回String,并且有具体页面可以跳转,那么就会被视图解析器解析
 3 @Controller
 4 public class HelloController  {
 5     @RequestMapping("/text")
 6     public String text(Model model){
 7         //封装数据
 8         model.addAttribute("msg","Hello,欢迎来到德莱联盟");
 9 
10         return "text";//会被视图解析器处理
11     }
12 }

 

jsp名字要  return 里面的值一样,不一样的话解析不匹配

 1 <body> 2 ${msg} 3 </body> 

 

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