Fork me on GitHub

SSM———访问controller404错误

搭建SSM项目时访问controller层时,显示404错误,404顾名思义:404页面是客户端在浏览网页时,服务器无法正常提供信息,或是服务器无法回应,且不知道原因所返回的页面。

1.首先怀疑是内部逻辑错误,先创建测试类,测试数据库是否连接成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package net.zzqd.mmall.test;

import net.zzqd.mmall.pojo.User;
import net.zzqd.mmall.service.IUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* @author zqnh
* @date 2019/7/26 on 12:53.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/applicationContext*.xml"})
public class Test1
{
@Autowired
private IUserService iUserService;
@Test
public void test()
{
User user = new User();
user.setPassword("root");
user.setUsername("root");
user.setAnswer("哈哈");
user.setQuestion("哈哈");
user.setPhone("15151515151");
user.setEmail("7151515@qq.com");
user.setId(50);
user.setRole(0);
iUserService.register(user);
if(iUserService.register(user)!= null)
{
System.out.println("注册成功");
}
else
{
System.out.println("注册失败");
}
}

}

数据库中插入了这条数据,数据库连接没问题。

访问controller,还是404

2.紧接着,查看web.xml中是否有DispatcherServlet

1
2
3
4
5
6
7
8
9
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

也有

3.看WEB-INF中dispatcher-servlet.xml中是否有,这是开启mvc模式

1
<mvc:annotation-driven/>

4.然后又看Controller层里的RequestMapping中的参数,也没问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package net.zzqd.mmall.controller.portal;

import net.zzqd.mmall.common.Const;
import net.zzqd.mmall.common.ResponseCode;
import net.zzqd.mmall.common.ServerResponse;
import net.zzqd.mmall.pojo.User;
import net.zzqd.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
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.ResponseBody;

import javax.servlet.http.HttpSession;

/**
* @author zqnh
* @date 2019/7/25 on 11:00.
*/
@Controller
@RequestMapping("/user/")
public class UserController
{
@Autowired
private IUserService iUserService;
/**
* 用户登陆
* @param username
* @return
*/

@RequestMapping(value = "login.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<User> login(String username, String password, HttpSession session)
{
ServerResponse<User> response = iUserService.login(username,password);
if(response.isSuccess())
{
session.setAttribute(Const.CURRENT_USER,response.getData());
}
return response;
}

@RequestMapping(value = "logout.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<String> logout(HttpSession session)
{
session.removeAttribute(Const.CURRENT_USER);
return ServerResponse.createBySuccess();
}

@RequestMapping(value = "register.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<String> register(User user)
{
return iUserService.register(user);
}

@RequestMapping(value = "check_valid.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<String> checkValid(String str,String type)
{
return iUserService.checkValid(str,type);
}

@RequestMapping(value = "get_user_info.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<User> getUserInfo(HttpSession session)
{
User user = (User) session.getAttribute(Const.CURRENT_USER);
if(user != null)
{
return ServerResponse.createBySuccess(user);
}
return ServerResponse.createByErrorMessage("用户未登录,无法获得当前用户信息");
}

@RequestMapping(value = "forget_get_question.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<String> forgetGetQuestion(String username)
{
return iUserService.selectQuestion(username);
}

@RequestMapping(value = "forget_check_answer.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<String> forgetCheckAnswer(String username,String question,String answer)
{
return iUserService.checkAnswer(username,question,answer);
}

@RequestMapping(value = "forget_reset_password.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<String> forgerRestPassword(String username,String passwordNew,String forgetToken)
{
return iUserService.forgetRestPassword(username,passwordNew,forgetToken);
}

@RequestMapping(value = "reset_reset_password.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<String> resetPassword(HttpSession session,String passwordOld,String passwordNew)
{
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null)
{
return ServerResponse.createByErrorMessage("用户未登录");
}
return iUserService.resetPassword(passwordOld,passwordNew,user);
}

@RequestMapping(value = "reset_imformation.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<User> update_information(HttpSession session,User user)
{
User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
if(currentUser == null)
{
return ServerResponse.createByErrorMessage("用户未登录");
}
user.setId(currentUser.getId());
user.setUsername(currentUser.getUsername());
ServerResponse<User> response = iUserService.updateInformation(user);
if(response.isSuccess())
{
session.setAttribute(Const.CURRENT_USER,response.getData());
}
return response;
}

@RequestMapping(value = "get_imformation.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<User> get_information(HttpSession session)
{
User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
if(currentUser == null)
{
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录,需要强制登陆");
}
return iUserService.getInformation(currentUser.getId());
}
}

5.然后看applicationContext.xml中是否配有视图解析器,由于这是接口测试,没有前台页面,故没影响

6.最后!!!,我发现Tomcat启动成功后,弹出的页面地址如下

因为我之前输入的地址都是

1
http://localhost:8088/user/login.do?username=root&password=root

然后我输入项目名

1
http://localhost:8088/mmall_war_exploded/user/login.do?user=root&password=root

重点来了,突然发现405错误,405错误,是请求错误,比如GET请求输入的是POST请求,或者是POST请求输入的是GET请求,然后我再Postman里测试,成功返回参数,大功告成。

1
http://localhost:8088/mmall_war_exploded/user/login.do

谨记:发现问题 分析问题 解决问题