Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringMVC控制器错误_Java_Spring_Jsp_Spring Mvc_Servlets - Fatal编程技术网

Java SpringMVC控制器错误

Java SpringMVC控制器错误,java,spring,jsp,spring-mvc,servlets,Java,Spring,Jsp,Spring Mvc,Servlets,从周五开始我就一直在修补这个错误,直到现在我还没有修复它。我的这个SpringMVC项目基于我去年完成的一项任务,这项任务很有效。并增加了一些新的东西。所以我有这个登录页面,我还没有添加安全性。我只是想看看,如果我做一些事务,比如检查数据库中是否存在用户,并将其转发到主页上,我的前端和BakcEnd是否会相遇。以下是一些代码片段: 对于TestController.java @Controller public class TestController { @Autowired private

从周五开始我就一直在修补这个错误,直到现在我还没有修复它。我的这个SpringMVC项目基于我去年完成的一项任务,这项任务很有效。并增加了一些新的东西。所以我有这个登录页面,我还没有添加安全性。我只是想看看,如果我做一些事务,比如检查数据库中是否存在用户,并将其转发到主页上,我的前端和BakcEnd是否会相遇。以下是一些代码片段:

对于TestController.java

@Controller
public class TestController {

@Autowired
private UserService userService;

@RequestMapping(value = "index")
public ModelAndView index(HttpServletRequest request, ModelMap model) {
    System.out.println("Index !");
    return new ModelAndView("index", "user", new User());
}

@RequestMapping(value = "login", method = RequestMethod.POST)
public ModelAndView login(HttpServletRequest request,
        HttpServletResponse res) {
    System.out.println("inside login");
    String userName = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println(userName);
    System.out.println(password);

    User user = new User();
    user.setUsername(userName);
    user.setPassword(password);

    if (userService.userLogin(user)) {
        return new ModelAndView("mainpage");
    } else {
        return new ModelAndView("index");
    }
}
jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page import="javax.servlet.http.HttpSession" %>
<html>
<head>
<title>KSPEAK CMS V1.0</title>
<!-- Bootstrap core CSS -->
<link href="<c:url value='/resources/bootstrap/css/bootstrap.min.css'/>"
rel="stylesheet">
<!-- Custom styles for this template -->
<link href="<c:url value='/resources/bootstrap/css/signin.css'/>"
rel="stylesheet">
<body>

<div class="container">

    <form:form class="form-signin" method="POST" action="login" commandName="user"  >
        <h2 class="form-signin-heading">Please sign in</h2>
        <label for="inputUserName" class="sr-only">UserName</label> <form:input
            type="text" name="username" class="form-control" path="username"
            placeholder="UserName" required="required" autofocus="autofocus"/> <label
            for="inputPassword" class="sr-only">Password</label> <form:input
            type="password" name="password" class="form-control" path="password"
            placeholder="Password" required="required"/>
        <input class="btn btn-lg btn-primary btn-block" type="submit" value="login">
    </form:form>
</div>
</body>
</head>
</html>
class User{

     private username; // name must match path in your form inside index.jsp
     private password;
     //getter && setter

   }

KSPEAK CMS V1.0
请登录
用户名密码
My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-a      pp_2_5.xsd">

<servlet>
    <servlet-name>myBatisServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springConfig.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>myBatisServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

myBatisServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/springConfig.xml
1.
myBatisServlet
*.html
My SpringConfig.xml(我在这一部分中编辑了一些内容,因为它会扰乱文章的布局,只是xmlns和模式位置)


运行应用程序时。它成功加载index.jsp并在控制器上通过我的第一个sysout。如下图所示:

假设我输入了一个现有用户,它应该重定向到主页。但这里有一张它的图片

它不符合我在控制器上设置的登录方法

到目前为止,我所做的一些事情是在@RequestMapping for login的值中添加/但仍然是相同的错误。尝试在url栏上手动键入登录名时出错,尝试在控制器中创建其他方法时出错。据我所知,它只能识别索引。所以是的,我想我需要一些帮助。像往常一样,我可以上传我的项目中你需要查看的任何文件。谢谢


myBatisServlet
*.html

这意味着将为匹配patter*.html的请求调用spring DispatcherServlet,但您将以登录名的形式向应用程序发送请求,DispatcherServlet将不处理该请求

你的问题有两种可能的解决办法

1。将*.html替换为/in
2.更改您的请求以匹配模式*.html

只是一个想法:

从该文件中删除.html


myBatisServlet
*.html

索引工作的原因是您在欢迎文件列表index.html中 当您使用基本url(yourwebpage/)访问应用程序时,它会查看欢迎文件列表,并尝试获取yourwebpage/index.html,您有一个“index”和url模式*.html的请求映射,它给出index.html,登录请求映射将变为login.html

因此,如果您离开*.html url模式,您的url将是@RequestMapping的属性 +.html


如果删除.html,它们将只是@RequestMapping的属性

我建议在下面尝试

您正在
index.jsp
中使用
commandName=“user”
,它有两个属性
username
password

根据JSP,在服务器端进行以下更改

1您应该有一个java类User.java,该类的属性与index.jsp中的path相同

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page import="javax.servlet.http.HttpSession" %>
<html>
<head>
<title>KSPEAK CMS V1.0</title>
<!-- Bootstrap core CSS -->
<link href="<c:url value='/resources/bootstrap/css/bootstrap.min.css'/>"
rel="stylesheet">
<!-- Custom styles for this template -->
<link href="<c:url value='/resources/bootstrap/css/signin.css'/>"
rel="stylesheet">
<body>

<div class="container">

    <form:form class="form-signin" method="POST" action="login" commandName="user"  >
        <h2 class="form-signin-heading">Please sign in</h2>
        <label for="inputUserName" class="sr-only">UserName</label> <form:input
            type="text" name="username" class="form-control" path="username"
            placeholder="UserName" required="required" autofocus="autofocus"/> <label
            for="inputPassword" class="sr-only">Password</label> <form:input
            type="password" name="password" class="form-control" path="password"
            placeholder="Password" required="required"/>
        <input class="btn btn-lg btn-primary btn-block" type="submit" value="login">
    </form:form>
</div>
</body>
</head>
</html>
class User{

     private username; // name must match path in your form inside index.jsp
     private password;
     //getter && setter

   }
2然后将此对象作为登录方法中的参数传递

 @RequestMapping(value = "login", method = RequestMethod.POST)
public ModelAndView login( @ModelAttribute User user) {
   // do your stuff
}

检查servlet映射。它只映射到
*.html
,但您的控制器URL是无扩展的。另一个人说的是:p编辑了这个。404错误未通过索引控制器。请使用/index访问索引页,或将web xml中的欢迎文件列表从index.html更改为将第一个解决方案索引。同样的结果404。没有通过索引。我已经有了一个pojo。并尝试了第2步。仍然在按下登录按钮之后。