在DispatcherServlet中找不到带有URI [/ LoginApp /]的HTTP请求的映射,名称为“java4s” [英] No mapping found for HTTP request with URI [/LoginApp/] in DispatcherServlet with name 'java4s'
问题描述
早些时候我的代码正在运行,但现在我遇到了这个错误
有时候代码有效,有时它不起作用。
Earlier my code was working but now I am facing this error Sometimes the code works and sometimes it does not work.
我的代码是
Java4sController.java
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";
}
}
web.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
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>
我的目录结构是
推荐答案
您的代码有几个拼写错误。
Your code has couple of typos.
1. 网址
不是制服
1.URL
is not uniform
-
displayForm.html
-
/ login
-
logoutForm
displayForm.html
/login
logoutForm
2.在 web.xml
映射不正确
-
< url-pattern> * .jsp< / url-pattern>
<url-pattern>*.jsp</url-pattern>
所以解决方案是维护一种格式或更改映射。
在 web.xml
In web.xml
< url-pattern> /< / url-pattern>
并使用任何格式,例如 *。html,*。行动,*。做
。
添加 /
到所有 RequestMappings
喜欢 /displayForm.html,/logoutForm
错误是找不到HTTP请求的映射,其中URI [/ LoginApp /]
And the Error is No mapping found for HTTP request with URI [/LoginApp/]
-
原因是
/
没有请求映射。编写一个方法来处理/
请求并在WEB-INF / pages下放置
。index.jsp
Reason is there is no request mapping for
/
. Write one method to handle/
request and placeindex.jsp
underWEB-INF/pages
.
@RequestMapping(value =/,method = RequestMethod.GET)
public String loadApp(UserDetails ud,HttpServletRequest request,HttpServletResponse response){
returnindex;
}
更新:
尝试在网络中提及 java4s-servlet.xml
.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/java4s-servlet.xml
</param-value>
</context-param>
这篇关于在DispatcherServlet中找不到带有URI [/ LoginApp /]的HTTP请求的映射,名称为“java4s”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!