5/23 16:34

This commit is contained in:
zyx 2023-05-23 16:34:29 +08:00
parent 009081ad82
commit 3e2ee2669b
7 changed files with 93 additions and 3 deletions

View File

@ -0,0 +1,18 @@
package cn.czyx007.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author : 张宇轩
* @createTime : 2023/5/23 - 15:18
*/
@Controller
@RequestMapping("/demo")
public class DemoController {
@GetMapping("/getUser")
public String getUser(){
return "show";
}
}

View File

@ -23,7 +23,7 @@ public class MyController implements Controller {
//1.封装数据 //1.封装数据
modelAndView.addObject("userName", "abc"); modelAndView.addObject("userName", "abc");
//2.封装视图 //2.封装视图
modelAndView.setViewName("WEB-INF/jsp/index.jsp"); modelAndView.setViewName("index");
return modelAndView; return modelAndView;
} }
} }

View File

@ -1,7 +1,11 @@
package cn.czyx007.mvc.controller; package cn.czyx007.mvc.controller;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
/** /**
@ -9,6 +13,7 @@ import org.springframework.web.servlet.ModelAndView;
* @createTime : 2023/5/23 - 11:40 * @createTime : 2023/5/23 - 11:40
*/ */
@Controller @Controller
@RequestMapping("/user")
public class UserController { public class UserController {
@RequestMapping("/user") @RequestMapping("/user")
public ModelAndView showUser(){ public ModelAndView showUser(){
@ -22,7 +27,39 @@ public class UserController {
//1.封装数据 //1.封装数据
modelAndView.addObject("userName", "blue"); modelAndView.addObject("userName", "blue");
//2.封装视图 //2.封装视图
modelAndView.setViewName("WEB-INF/jsp/index.jsp"); modelAndView.setViewName("index");
return modelAndView; return modelAndView;
} }
// @RequestMapping("/getUser")
@GetMapping({"/getUser", "/findUser", "/user01"})
public String getUser(Model model){
model.addAttribute("address", "北京");
return "show";
}
@GetMapping("/show")
public String show(
@RequestParam(value = "user", required = false, defaultValue = "liuyan") String userName,
@RequestParam(value = "age", required = false, defaultValue = "20") Integer age,
@RequestParam(value = "address", required = true) String address
){
System.out.println("userName = " + userName);
System.out.println("age = " + age);
System.out.println("address = " + address);
return "index";
}
@PostMapping ("/showPost")
public String showPost(
@RequestParam(value = "user", required = false, defaultValue = "liuyan") String userName,
@RequestParam(value = "age", required = false, defaultValue = "20") Integer age,
@RequestParam(value = "address", required = true) String address
){
System.out.println("userName = " + userName);
System.out.println("age = " + age);
System.out.println("address = " + address);
return "index";
}
} }

View File

@ -11,9 +11,14 @@
http://www.springframework.org/schema/context/spring-context-4.2.xsd "> http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!--此处id指要配置拦截的url路径--> <!--此处id指要配置拦截的url路径-->
<!-- <bean id="/myController" class="cn.czyx007.mvc.controller.MyController"/>--> <bean id="/myController" class="cn.czyx007.mvc.controller.MyController"/>
<context:component-scan base-package="cn.czyx007.mvc.*"/> <context:component-scan base-package="cn.czyx007.mvc.*"/>
<mvc:annotation-driven/> <mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans> </beans>

View File

@ -13,5 +13,6 @@
<body> <body>
<%--el表达式--%> <%--el表达式--%>
${userName} ${userName}
<h1> >>>>这里是index.jsp<<<< </h1>
</body> </body>
</html> </html>

View File

@ -0,0 +1,16 @@
<%--
Created by IntelliJ IDEA.
User: zyx
Date: 2023/5/23
Time: 14:25
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${address}
</body>
</html>

View File

@ -23,4 +23,17 @@
<url-pattern>/</url-pattern> <url-pattern>/</url-pattern>
</servlet-mapping> </servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app> </web-app>