gpt4 book ai didi

java - 在名称为 'java4s' 的 DispatcherServlet 中找不到具有 URI [/LoginApp/] 的 HTTP 请求的映射

转载 作者:行者123 更新时间:2023-11-29 04:41:25 25 4
gpt4 key购买 nike

之前我的代码可以正常工作,但现在我遇到了这个错误代码有时有效,有时无效。

我的代码是

Java4sController.java

package java4s;

import javax.validation.Valid;
import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.*;
import javax.servlet.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.sql.*;
//import org.h2.jdbcx.JdbcConnectionPool;


//import org.springframework.security.core.context.SecurityContextHolder;


@Controller
public class Java4sController {

HttpSession session;
String uname="",pass="";
Statement stmt;
Connection conn;

@RequestMapping(value="displayForm.html", method=RequestMethod.GET)
public String helloWorld(UserDetails ud,HttpServletRequest request,HttpServletResponse response) {
return "loginPage";
}



@RequestMapping("/login")
public String loginCheck(@Valid UserDetails userDetails, BindingResult result, ModelMap model,HttpServletRequest request,HttpServletResponse response)throws Exception
{
System.out.println("In the login page");

String username=userDetails.getUser();
String password=userDetails.getPassword();

if (result.hasErrors()) {
return "loginPage";
}
try {
Class.forName("org.h2.Driver");
conn = DriverManager.
getConnection("jdbc:h2:file:C:/Users/patilv/test", "sa", "");
// add application code here
if(conn!=null) {
System.out.println("No connection");
}

stmt=conn.createStatement();
String sql="Select * FROM table1 WHERE username='"+username+"' AND password='"+password+"' ";
ResultSet rs=stmt.executeQuery(sql);




while(rs.next())
{
uname=rs.getString("username");
pass=rs.getString("password");


if (result.hasErrors()) {
return "loginPage";
}

else if(username.equals(uname)&&password.equals(pass))
{
//session.invalidate();
response.setHeader("Cache-Control","no-cache");
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires", 0);
response.setHeader("Pragma","no-cache");
session = request.getSession(); // create session

session.setAttribute("username", username);
session.setAttribute("password", password);
//String str1=(String)session.getAttribute(username);

model.addAttribute("lfobj", userDetails);
//model.addAttribute(str1);
conn.close();

return "success";
}



else
{
conn.close();
return "loginPage";
}
}
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();

}//end finally try
}//end try
return "abcd";
}

@RequestMapping(value="logoutform", method=RequestMethod.GET)
public String helloWorld1(UserDetails ud,HttpServletRequest request,HttpServletResponse response) {

System.out.println("\nabcdef\n");
response.setHeader("Cache-Control","no-cache");
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires", 0);
response.setHeader("Pragma","no-cache");
String username=ud.getUser();
// String password=ud.getPassword();

HttpSession session1;
session1=request.getSession(false);
System.out.println("Session :"+session1.getAttribute("username"));
if(session1!=null)
{
//session1.removeAttribute(username);
session1.invalidate();
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
cookie.setMaxAge(0);
cookie.setValue(null);
cookie.setPath("/");
response.addCookie(cookie);
}
System.out.println("invalidating session");
//session1.invalidate();
}

return "logoutsuccess";
}

}

网络.xml

<?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/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
<servlet>
<servlet-name>java4s</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>java4s</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

java4s-servlet.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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="java4s" />
<mvc:annotation-driven />
<!-- <mvc:resources mapping="/scripts/**" location="/WEB-INF/" /> -->


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="props" />
</bean>

</beans>

我的目录结构是

enter image description here

最佳答案

您的代码有几个拼写错误。

1. URL 不统一

  • displayForm.html
  • /login
  • logoutForm

2.在 web.xml 映射不正确

  • <url-pattern>*.jsp</url-pattern>

所以解决方案是保持一种格式或更改映射。

web.xml

<url-pattern>/</url-pattern> 并使用任何格式,如 *.html,*.action,*.do .

添加/所有 RequestMappings喜欢/displayForm.html,/logoutForm

并且错误是没有找到具有 URI [/LoginApp/] 的 HTTP 请求的映射

  • 原因是 / 没有请求映射.写一个方法来处理 /请求和地点 index.jspWEB-INF/pages 下.

    @RequestMapping(value="/", method=RequestMethod.GET)
    public String loadApp(UserDetails ud,HttpServletRequest request,HttpServletResponse response) {
    return "index";
    }

更新:

尝试提及 java4s-servlet.xml in web.xml

   <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/java4s-servlet.xml
</param-value>
</context-param>

关于java - 在名称为 'java4s' 的 DispatcherServlet 中找不到具有 URI [/LoginApp/] 的 HTTP 请求的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39073144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com