基於SSM的單點登陸05
阿新 • • 發佈:2018-06-18
param loader 解析 tar func str -name 配置 index.jsp
springmvc.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc ="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" >
9 <!-- 解析properties文件的工具類 -->
10 <context:property-placeholder location="classpath:*.properties"/>
11
12 <!-- 掃描controller組件 -->
13 <context:component-scan base-package="io.guangsoft.market.controller"/>
14
15 <!-- 開啟註解驅動 -->
16 <mvc:annotation-driven />
17
18 <!-- 配置視圖解析 -->
19 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20 <property name="prefix" value="/jsp/" />
21 <property name="suffix" value=".jsp" />
22 </bean>
23 <!-- 資源映射 -->
24 <mvc:resources location="/css/" mapping="/css/**"/>
25 <mvc:resources location="/js/" mapping="/js/**"/>
26 <mvc:resources location="/images/" mapping="/images/**"/>
27 </beans>
web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.5"
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
7 <!-- 啟動spring -->
8 <context-param>
9 <param-name>contextConfigLocation</param-name>
10 <param-value>classpath:spring-*.xml</param-value>
11 </context-param>
12 <listener>
13 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
14 </listener>
15
16 <!-- 解決post亂碼 -->
17 <filter>
18 <filter-name>CharacterEncodingFilter</filter-name>
19 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
20 <init-param>
21 <param-name>encoding</param-name>
22 <param-value>utf-8</param-value>
23 </init-param>
24 </filter>
25 <filter-mapping>
26 <filter-name>CharacterEncodingFilter</filter-name>
27 <url-pattern>/*</url-pattern>
28 </filter-mapping>
29
30 <!-- 啟動springmvc -->
31 <servlet>
32 <servlet-name>market</servlet-name>
33 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
34 <!-- contextConfigLocation不是必須的, 如果不配置contextConfigLocation, springmvc的配置文件默認在:WEB-INF/servlet的name+"-servlet.xml" -->
35 <init-param>
36 <param-name>contextConfigLocation</param-name>
37 <param-value>classpath:springmvc.xml</param-value>
38 </init-param>
39 <load-on-startup>1</load-on-startup>
40 </servlet>
41 <servlet-mapping>
42 <servlet-name>market</servlet-name>
43 <url-pattern>/</url-pattern>
44 </servlet-mapping>
45
46 <welcome-file-list>
47 <welcome-file>index.html</welcome-file>
48 <welcome-file>index.htm</welcome-file>
49 <welcome-file>index.jsp</welcome-file>
50 <welcome-file>default.html</welcome-file>
51 <welcome-file>default.htm</welcome-file>
52 <welcome-file>default.jsp</welcome-file>
53 </welcome-file-list>
54 </web-app>
PageController.java
1 package io.guangsoft.market.controller;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.ui.Model;
5 import org.springframework.web.bind.annotation.RequestMapping;
6
7 /**
8 * 頁面跳轉
9 */
10 @Controller
11 public class PageController {
12
13 @RequestMapping("/login")
14 public String showLogin(String redirect, Model model) {
15 model.addAttribute("redirect", redirect);
16 return "login";
17 }
18
19 @RequestMapping("/register")
20 public String showRegister() {
21 return "register";
22 }
23 }
UserController.java
1 package io.guangsoft.market.controller;
2
3 import javax.servlet.http.HttpServletRequest;
4 import javax.servlet.http.HttpServletResponse;
5
6 import io.guangsoft.market.util.bean.GResult;
7 import io.guangsoft.market.dao.bean.TbUser;
8 import io.guangsoft.market.service.UserService;
9 import io.guangsoft.market.util.utils.CookieUtil;
10 import io.guangsoft.market.util.utils.GResultUtil;
11 import org.apache.commons.lang3.StringUtils;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.beans.factory.annotation.Value;
14 import org.springframework.http.converter.json.MappingJacksonValue;
15 import org.springframework.stereotype.Controller;
16 import org.springframework.web.bind.annotation.PathVariable;
17 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.RequestMethod;
19 import org.springframework.web.bind.annotation.ResponseBody;
20
21
22 /**
23 * 用戶登錄服務controller
24 */
25 @Controller
26 @RequestMapping("/user")
27 public class UserController {
28
29 @Autowired
30 private UserService userService;
31
32 @Value("${REDIS_USER_SESSION_KEY}")
33 private String cookieName;
34
35 /**
36 * 用戶註冊數據校驗接口
37 */
38 @RequestMapping(value="/check/{param}/{type}",method=RequestMethod.GET)
39 @ResponseBody
40 public Object checkData(@PathVariable String param,@PathVariable int type,String callback){
41 GResult result = null;
42 //數據校驗
43 if(type != 1 && type != 2 && type != 3){
44 result = GResultUtil.fail(-1, "參數類型有誤!");
45 }else{
46 //調用業務層做數據校驗
47 result = this.userService.userParamCheck(param, type);
48 }
49 //判斷是否使用jsonp調用方式
50 if(callback != null && callback.length() > 0){
51 MappingJacksonValue mapping = new MappingJacksonValue(result);
52 mapping.setJsonpFunction(callback);
53 return mapping;
54 }
55 return result;
56 }
57
58 /**
59 * 用戶註冊接口
60 */
61 @RequestMapping(value="/register",method=RequestMethod.POST)
62 @ResponseBody
63 public GResult userRegister(TbUser user){
64 try{
65 return this.userService.userRegister(user);
66 }catch(Exception e){
67 e.printStackTrace();
68 return GResultUtil.fail(-1, "註冊失敗. 請校驗數據後請再提交數據!");
69 }
70 }
71
72 /**
73 * 用戶登錄接口
74 */
75 @RequestMapping(value="/login",method=RequestMethod.POST)
76 @ResponseBody
77 public GResult userLogin(String username,String password,HttpServletRequest request,HttpServletResponse repsonse){
78 try{
79 GResult result = this.userService.userLogin(username, password);
80 if(result.getgCode() == 0) {
81 CookieUtil.setCookie(request, repsonse, cookieName, result.getgData().toString());
82 }
83 return result;
84 }catch(Exception e){
85 e.printStackTrace();
86 return GResultUtil.fail(-1, e.getMessage());
87 }
88 }
89
90 /**
91 * 根據token查詢用戶
92 */
93 @RequestMapping(value="/token/{token}",method=RequestMethod.GET)
94 @ResponseBody
95 public Object findUserByToken(@PathVariable String token,String callback){
96 GResult result = this.userService.findUserByToken(token);
97 //判斷是否含有jsonp
98 if(!StringUtils.isBlank(callback)){
99 MappingJacksonValue mapping = new MappingJacksonValue(result);
100 mapping.setJsonpFunction(callback);
101 return mapping;
102 }
103 return result;
104 }
105 }
基於SSM的單點登陸05