MVC模式:
1、目錄結構:
2、XML配置文件
spring-config.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:annotation-config/>
<context:component-scan base-package="com.he"/>
</beans>
spring-mvc配置文件:
prefix前綴
suffix後綴
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<context:component-scan base-package="com.he"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
<property name="contentType" value="UTF-8"/>
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
</bean>
</beans>
3、配置UserController類
package com.he.controller;
import com.he.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* @author 賀文傑
* 2018/8/1 17:16
*/
@Controller
@RequestMapping("user")
public class UserController {
@RequestMapping("/test")
public String testUser(){
return "user";
}
@RequestMapping("/demo")
public String demoUser(){
return "user";
}
}
重定向:
@RequestMapping("/demo")
public String demoUser(ModelMap modelMap){
User user=new User();
user.setId(18);
user.setName("賀文傑");
modelMap.addAttribute("user",user);
// 重定向
return "redirect:/demo/test";
}
使用get請求:
@RequestMapping("/test/{id}/{name}")
public ModelAndView testUser(ModelMap modelMap, @PathVariable Integer id,@PathVariable String name){
System.out.println(id);
System.out.println(name);
ModelAndView model =new ModelAndView();
model.setViewName("user");
User user=new User();
user.setId(18);
user.setName("賀文傑");
model.addObject("user",user);
return model;
}
get 傳參兩種:
1、URL拼接字符串
2、用/ 指定ID/
使用post傳遞參數:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/8/1
Time: 20:09
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>
<form method="post" action="/user/test">
<input type="text" name="id">
<input type="text" name="name">
<input type="submit" value="提交">
</form>
</body>
</html>
@RequestMapping("/test")
public String testUser(ModelMap modelMap,User user){
modelMap.addAttribute("user",user);
return "user";
}