SpringMVC学习之——03 对servlet API以及JSON的支持

版权声明:为中华之崛起而努力! https://blog.csdn.net/Kj_Gym/article/details/81195220

下面是一个模拟User用户简单登录的过程,使用HttpSession存下用户名userName和密码password。转化成JSON格式,通过超链接“ajax”获取到JSON格式的用户名和密码。

1. Model包里的User,一个id,一个名字userName,一个密码password。不再写出。

2. Controller包里的UserController类

package com.kjgym.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kjgym.model.User;

@Controller
@RequestMapping("/user")
public class UserController {
	@RequestMapping("/login")
	public String login(HttpServletRequest request,HttpServletResponse response){
		System.out.println("---登录验证---");
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		Cookie cookie = new Cookie("user", userName+"-"+password);
		cookie.setMaxAge(1*60*10*24*7);
		User currentUser = new User(userName,password);
		response.addCookie(cookie);
		HttpSession session = request.getSession();
		session.setAttribute("currentUser", currentUser);
		return "redirect:/main.jsp";
	}
	// 下面这个是将User转化为JSON格式,但是一般不这样搞
	/**
	 * ResponseBody这个东西自动把User转化为JSON格式
	 * @return
	 */
	@RequestMapping("/ajax")
	public @ResponseBody User ajax(){
		User user = new User("张三","123");
		return user;
	}
}

3. 配置spring-mvc.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 使用注解的包,包括子集 -->
    <context:component-scan base-package="com.kjgym"/>
	
	<!-- 支持对象与JSON的转换 -->
	<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"></property>
	</bean>

</beans>

4.login.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>登录界面</title>
</head>
<body>
<!-- 下面这一行是为了测试 -->
<a href="${pageContext.request.contextPath }/user/ajax.do">测试ajax</a>
<form action="${pageContext.request.contextPath }/user/login.do" method="post">
	<table>
		<tr>
			<td>用户名:</td>
			<td><input type="text" name="userName"/></td>
		</tr>
		<tr>
			<td>密码:</td>
			<td><input type="password" name="password"/></td>
		</tr>
		<tr>
			<td><input type="submit" value="登录"/></td>
			<td><input type="reset" value="重置"/></td>
		</tr>
	</table>
</form>
</body>
</html>

5. main.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>Insert title here</title>
</head>
<body>
Main.jsp 欢迎<span style="color: red">${currentUser.userName }</span> 登录
</body>
</html>

6.web.xml文件,不需要多配置什么,一个spring基本配置,一个filter解决乱码问题就够了

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>SpringMvc2</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-mvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
	<filter>
		<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
</web-app>

猜你喜欢

转载自blog.csdn.net/Kj_Gym/article/details/81195220
今日推荐