标签:image inf 使用 style base efault ati require control
标题
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; /* * 这是控制层 */ @Controller public class MyController { @RequestMapping(value="/hello",method=RequestMethod.GET) public ModelAndView hello(@RequestParam(name="id",required=false)Integer id){ //可以简化成 (int id) System.out.println(id); System.out.println("hello GET Spring"); return new ModelAndView("/comm/hello"); } @RequestMapping(value="/hello",method=RequestMethod.POST) public ModelAndView hello2(@RequestParam(name="title")String title){ System.out.println(title); System.out.println("hello POST Spring"); return new ModelAndView("/comm/hello"); } }
注解 解释 说明
@Controller 控制器 普通类
@RequestMapping 路由 value = {""} -> 匹配多路路由 一般映射唯一
@RequestMapping 路由 value = {""} -> 匹配多路路由 一般映射唯一
method = "" -> RequestMethod.GET | RequestMethod.POST
注意:路由命名必须唯一 GET和POST可以共享
produces ={""} -> Ajax中文处理 "application/json;charset=utf-8"
@ResponseBody 异步请求 返回数据 -> json串
@RequestParam 参数映射 name |value -> get映射 id=1001 | post 映射 <input name="id" />
required -> true 参数必须(默认) | false 参数省略
defaultValue -> 参数省略通过默认值填充
@ModelAttribute 模型映射 初始化对象 -> 类 对其进行反射对象 属性注值 key->value作用域
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.uplooking.controller"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="root" value="${pageContext.request.contextPath}"></c:set>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
</head>
<body>
<a href="${root}/hello">hello</a>
<hr/>
<form action="${root}/hello" method="post">
<input type="text" name="title">
<input type="submit" value="hello">
</form>
</body>
</html>
hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>hello</title> </head> <body> springmvc我来了 </body> </html>
运行结果:
点第一个hello:
控制台
第二个hello:
结果:
控制台乱码
标签:image inf 使用 style base efault ati require control
原文地址:https://www.cnblogs.com/lanbao5339/p/14098862.html