Browse Source

springMVC 转发与重定向

zhang 2 years ago
parent
commit
e5703b06ce

+ 15 - 2
springMvc/src/main/java/com/controller/UserController.java

@@ -3,11 +3,24 @@ package com.controller;
3 3
 import org.springframework.stereotype.Controller;
4 4
 import org.springframework.web.bind.annotation.RequestMapping;
5 5
 
6
+import javax.servlet.http.HttpServletRequest;
7
+import javax.servlet.http.HttpServletResponse;
8
+import java.io.IOException;
9
+
6 10
 @Controller
7 11
 public class UserController {
8 12
     @RequestMapping("/quick")
9
-    public String save() {
13
+    public String quick() {
10 14
         System.out.println("userControllerRunning1...");
11
-        return "success.jsp";
15
+//        return "forward:success.jsp";
16
+        // 加了前后缀 完整链接应该是 forward:/template/success.jsp
17
+        return "redirect:http://baidu.com";
18
+    }
19
+    @RequestMapping("/save")
20
+    public void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
21
+        // 直接返回字符串
22
+        response.getWriter().println("save");
23
+//        return "forward:success.jsp";
24
+
12 25
     }
13 26
 }

+ 13 - 1
springMvc/src/main/resources/Spring-mvc.xml

@@ -2,7 +2,19 @@
2 2
 <beans xmlns="http://www.springframework.org/schema/beans"
3 3
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 4
        xmlns:context="http://www.springframework.org/schema/context"
5
+       xmlns:mvc="http://www.springframework.org/schema/mvc"
5 6
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
6 7
 <!--    controller组件扫描-->
7
-    <context:component-scan base-package="com.controller"></context:component-scan>
8
+<!--    <context:component-scan base-package="com.controller"></context:component-scan>-->
9
+<!--    只扫描 com下的@Controller注解-->
10
+    <context:component-scan base-package="com">
11
+        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
12
+    </context:component-scan>
13
+
14
+<!--    <mvc:annotation-driven></mvc:annotation-driven>-->
15
+<!--    配置内部资源解析器-->
16
+<!--    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
17
+<!--        <property name="prefix" value="/template/"></property>-->
18
+<!--        <property name="suffix" value=".jsp"></property>-->
19
+<!--    </bean>-->
8 20
 </beans>

+ 1 - 1
springMvc/src/main/webapp/WEB-INF/web.xml

@@ -26,7 +26,7 @@
26 26
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27 27
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
28 28
   <welcome-file-list>
29
-    <welcome-file>index.jsp</welcome-file>
29
+    <welcome-file>/template/index.jsp</welcome-file>
30 30
   </welcome-file-list>
31 31
   <listener>
32 32
 <!--    <listener-class>com.listener.ContextLoadListener</listener-class>-->

springMvc/src/main/webapp/index.jsp → springMvc/src/main/webapp/template/index.jsp


springMvc/src/main/webapp/success.jsp → springMvc/src/main/webapp/template/success.jsp