项目结构:
直接看代码:
spring-servlet.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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 配置文件的扫描路径 -->
<context:component-scan base-package="com.jadeon.action"/>
<!-- 配置自动扫描 -->
<mvc:annotation-driven/>
<!-- 默认支持静态文件 -->
<mvc:default-servlet-handler/>
<!-- 视图加载器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 支持jstl -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 返回的界面路径 -->
<property name="prefix" value="/WEB-INF/"/>
<!-- 返回的界面文件后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>springMVC_1</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</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>
action:
package com.jadeon.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import com.jadeon.entity.UserInfo;
/***
* @Controller : 标注UserAction为控制器
* @RequestMapping : 控制器如何访问, 方法如何访问
* @author Administrator
*
*/
@Controller
@RequestMapping(value = "/user")
public class UserAction {
@RequestMapping(value = "/firstRequst")
public String login(Model model) {
System.out.println("login...");
model.addAttribute("username", "jadeon1");
return "jsp/success";
}
@RequestMapping(value = "/secondRequst/*", method = RequestMethod.POST)
public String visitWithPathVariablePattern(Model model, UserInfo info) {
System.out.println(info.getAge());
UserInfo info1 = new UserInfo();
info1.setAge(info.getAge());
model.addAttribute("info", info1);
return "jsp/success";
}
@RequestMapping(value = "/threeRequst")
public String servletAPI(HttpServletRequest request, HttpServletResponse response, HttpSession session,
Model model) {
System.out.println(request.getMethod());
String method = request.getMethod();
model.addAttribute("method", method);
return "jsp/success";
}
@RequestMapping(value = "/fourRequst")
public String webRequest(WebRequest request,Model model){
System.out.println(request.getContextPath());
String getContextPath = request.getContextPath();
model.addAttribute("getContextPath", getContextPath);
return "jsp/success";
}
@RequestMapping(value="/fiveRequst/{username}")
public String visitWithPathVariable(@PathVariable String username,Model model){
System.out.println(username);
String uname = username;
model.addAttribute("uname", uname);
return "jsp/success";
}
/***
* post请求
* @RequestParam 映射请求参数(required)是否必须传参数,defaultValue 默认值
* @param info.age
* @param model
* @return
*/
@RequestMapping("/sixRequst")
public String postRequest(UserInfo info,Model model){
System.out.println(info.getUsername()+"--"+info.getLoginame());
UserInfo info1 = new UserInfo();
info1.setUsername(info.getUsername());
info1.setLoginame(info.getLoginame());
model.addAttribute("info", info1);
return "jsp/success";
}
@RequestMapping("/sevenRequst")
public String requestBody(@RequestBody String body,Model model){
//这个是一个请求映射方法,需要使用ajax,数据类型引用json类型( dataType: "application/json")
System.out.println(body);
return "jsp/success";
}
@RequestMapping("/eightRequst")
@ResponseBody
public String responseBody() {
return "{username:admin}";
}
@ModelAttribute
public void startLog() {
System.out.println("开始记录...");
}
}
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">
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<title>Insert title here</title>
</head>
<body>
<h1>SpringMVC 8种传值方式</h1>
<h2>First Request:<a href="user/firstRequst">First</a></h2>
<h2>Second Request:</h2>
<form action="user/secondRequst/*" method="post" name="info">
Age:<input type="text" name="age" value="2">
<input type="submit" value="Second">
</form>
<h2>Three Request:<a href="user/threeRequst">Three</a></h2>
<h2>Four Request:<a href="user/fourRequst">Four</a></h2>
<h2>Five Request:<a href="user/fiveRequst/jadeon-5">Five</a></h2>
<h2>Six Request:</h2>
<form action="user/sixRequst" method="post" name="info">
username:<input type="text" name="username">
loginame:<input type="text" name="loginame">
<input type="submit" value="登录">
</form>
<br/>
<h2>Seven Request:<a href="javascript:void(0)" onclick="seven()">Seven</a> </h2>
<h2>Eight Request:<a href="user/eightRequst">Eight</a></h2>
</body>
</html>