以前我的代码可以正常工作,但是现在我遇到此错误。
我的代码是
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
<?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>
My directory structure is
您的代码有两个错字。
1. URL 不统一
2. displayForm.html
3. /login
4. logoutForm
2.In web.xml
映射是不正确
<url-pattern>*.jsp</url-pattern>
在web.xml
<url-pattern>/</url-pattern>
并使用任何格式,例如
.html,.action,*.do`。
添加/
到所有RequestMappings
喜欢/displayForm.html,/logoutForm
并且错误是未找到具有URI [/ LoginApp /]的 HTTP请求的映射
原因是没有的请求映射/
。编写一种方法来处理/
请求并放置index.jsp
在下WEB-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>
在Spring项目中, 警告:org。springframework。网状物servlet。PageNotFound-在名为“appServlet”的DispatcherServlet中找不到URI为[/]的HTTP请求的映射 错误不断发生。我不知道为什么会发生这些错误。 请你帮我... servlet上下文。xml 正如你在下面看到的,我指定了@Controller。 BoardControll
我想配置文件中缺少一些东西,但我看不到。 当我放入浏览器-->localhost:8080/procura/notifications-->我在控制台中得到的是-->org.springframework.web.servlet.DispatcherServlet noHandlerFound警告:在名为“DispatcherServlet”的DispatcherServlet中没有找到URI[/
这是我的代码。我不知道这有什么问题。
问题内容: 我已经检查了关于stackoverflow的几乎所有相关文章,但是我无法解决我的问题。 Here is the code: web.xml: spring-servlet.xml: myController: Web Pages/index.jsp: Web Pages/WEB-INF/jsp/hello.jsp: 因此,当我启动该应用程序时,index.jsp已正确加载,但是当我单击
早上,我已经检查了这个问题的大部分答案(在名为DispatcherServlet的DispatcherServlet中找不到URI为的HTTP请求的映射),以及(在名为“DispatcherServlet”的DispatcherServlet中找不到URI为[/ChickenTest/index]的HTTP请求的映射),但我仍然得到“在名为“bmoa”的DispatcherServlet中找不到U
这是我的Spring安全豆 这是我的MVC-dispatcher servlet 这是我的web.xml