首页 新闻 会员 周边

程序运行没报错,但是访问就是404

0
悬赏园豆:100 [已解决问题] 解决于 2019-08-08 17:10
~笑春风~的主页 ~笑春风~ | 菜鸟二级 | 园豆:252
提问于:2019-08-08 14:44
< >
分享
最佳答案
0

项目设置访问名是Message么,web.xml看下项目入口

收获园豆:100
小光 | 小虾三级 |园豆:1766 | 2019-08-08 14:53

这是web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
http://www.springmodules.org/schema/cache/springmodules-cache.xsd
http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
">
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- Spring监听器 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 编码过滤器 -->
<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <async-supported>true</async-supported>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<!-- shiro过滤器定义 -->
<filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 -->
        <param-name>targetFilterLifecycle</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 添加对springmvc的支持 -->
<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>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 14:57

@一只小小弱鸡:
1.右键项目——>Properties——>Web Project Settings看一下这里面填的什么
2.web.xml把这个加上去然后访问:http://localhost:8080/Message
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
</welcome-file-list>

小光 | 园豆:1766 (小虾三级) | 2019-08-08 15:07

@小光: Web Project Settings填的/Message,web.xml改了还是404

~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 15:14

@一只小小弱鸡: 看一下你login.jsp的路径

小光 | 园豆:1766 (小虾三级) | 2019-08-08 15:18

@小光: <%@ 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>
<form action="${pageContext.request.contextPath }/login" method="post">
用户名:<input type="text" name="username" value="${user.username }"/><br/>
密码:<input type="password" name="password" value="${user.password }"><br/>
<input type="submit" value="login"/><br>
<font color="red">${errorMsg }</font>
</form>
</body>
</html>

~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 15:31

@一只小小弱鸡: login.jsp在项目中的位置,不是代码

小光 | 园豆:1766 (小虾三级) | 2019-08-08 15:38

@小光: 在src/main/webapp/WEB-INF/views下,拿出来放在webapp下我也试过。。。

~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 15:41

@小光:

~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 15:44

@一只小小弱鸡:
web.xml加上这段代码

<welcome-file-list>
        <welcome-file>/WEB-INF/views/login.jsp</welcome-file>
    </welcome-file-list>

访问:http://localhost:8080/Message

小光 | 园豆:1766 (小虾三级) | 2019-08-08 15:44

对啊,我就是这样加的。。

~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 15:45

@小光:

~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 15:47

@一只小小弱鸡: 那你试试在views下新建一个html,把web.xml里面的login.jsp换成新建的html试一下是不是login.jsp的问题

小光 | 园豆:1766 (小虾三级) | 2019-08-08 15:47

@小光: 还是不行。。。

~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 15:53

@一只小小弱鸡: 厉害了。。你确定把这个项目添加到tomcat上了么。。。

小光 | 园豆:1766 (小虾三级) | 2019-08-08 15:56

@一只小小弱鸡: http://localhost:8080/看下tomcat主页正常么

小光 | 园豆:1766 (小虾三级) | 2019-08-08 15:59

@一只小小弱鸡: 实在不行就新建一个web项目,页面肯定可以访问,然后根据你这个项目的配置往新项目里面加,比如加上views文件夹或者web.xml的配置文件,改一个地方就访问一下新项目的页面,什么时候访问不了了就找到问题了

小光 | 园豆:1766 (小虾三级) | 2019-08-08 16:08

@小光: 好的谢谢

~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 16:13

@小光: 你说的对,重新试了一下,是web.xml的问题

~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 17:10
其他回答(2)
0

检查下路由是怎么设置的。

会长 | 园豆:12401 (专家六级) | 2019-08-08 14:51
0

运行没有报错和访问出现错误没有关系,访问报404就是你的访问路径不对,或者你的程序的编写有问题,比喻你的一个表单提交,提交到了根本没有的一个页面上

键盘小侠 | 园豆:175 (初学一级) | 2019-08-08 14:52

这是controller层和jsp页面
package com.yang.controller;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.yang.entity.User;
/**
  
 
created by yangqing on 2018年2月19日 下午9:48:55
 */
@Controller
public class UserController {

@RequestMapping("/login")
public String login(User user,HttpServletRequest request){      
    //获取当前用户
    Subject subject=SecurityUtils.getSubject();
    UsernamePasswordToken token=new UsernamePasswordToken(user.getUsername(), user.getPassword());
    try{
        //为当前用户进行认证,授权
        subject.login(token);
        request.setAttribute("user", user);
        return "success";           
    }catch(Exception e){
        e.printStackTrace();
        request.setAttribute("user", user);
        request.setAttribute("errorMsg", "用户名或密码错误!");
        return "login";
    }
}

@RequestMapping("/teacher")
public String index() {
    return "index";
}

}
<%@ 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>
<form action="${pageContext.request.contextPath }/login" method="post">
用户名:<input type="text" name="username" value="${user.username }"/><br/>
密码:<input type="password" name="password" value="${user.password }"><br/>
<input type="submit" value="login"/><br>
<font color="red">${errorMsg }</font>
</form>
</body>
</html>

支持(0) 反对(0) ~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 15:00

@一只小小弱鸡: 你把你访问的路径发出来,看看

支持(0) 反对(0) 键盘小侠 | 园豆:175 (初学一级) | 2019-08-08 15:03

@博了个itdjl的客: http://localhost:8080/Message/login.jsp

支持(0) 反对(0) ~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 15:04

@一只小小弱鸡: 你是在第一次请求就出现404吗?还是填写了login.jsp后出现404,如果是之后的话,你把那个${pageContext.request.contextPath}去掉试试看,如果是第一次请求,一定要直接把login.jsp放在webroot目录下面。还有可能你修改了项目部署时的名称

支持(0) 反对(0) 键盘小侠 | 园豆:175 (初学一级) | 2019-08-08 15:11

@博了个itdjl的客: 第一次就404了

支持(0) 反对(0) ~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 15:15

@一只小小弱鸡: 项目访问设置的是叫Message吗?

支持(0) 反对(0) 键盘小侠 | 园豆:175 (初学一级) | 2019-08-08 15:16

@博了个itdjl的客: 是啊,项目名就是Message,Web Project Settings中是/Message

支持(0) 反对(0) ~笑春风~ | 园豆:252 (菜鸟二级) | 2019-08-08 15:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册