您好我一直在尝试使用我的jsp映射我的控制器但是它会出错并显示一个旧URL。我已多次尝试清理项目并更改了我的web.xml。
下面的是我的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/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Sphibernate</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>mvc-dispatcher </servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
以下是我的Controller类:
package com.lnt.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.lnt.services.AuthenticateServices;
@Controller
@RequestMapping("Login.htm")
public class LoginController {
//@Autowired
AuthenticateServices authenticateService = new AuthenticateServices();
@RequestMapping(method=RequestMethod.POST)
public ModelAndView processCredentials(HttpServletRequest req, HttpServletResponse res)
{
String userName =req.getParameter("userName") ;
String password = req.getParameter("password");
System.out.println("into login controller");
String message = "Invalid credentials";
List<String> userdetails = new ArrayList<String>();
userdetails = authenticateService.verifyUserNameAndPassword(userName, password);
if((userdetails.get(0))!= userName && (userdetails.get(1))!= password )
{
message = "welcome" + userName ;
}
return new ModelAndView("results", "message", message) ;
}
}
这是我的JSP:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Login.htm" method = "post">
<input type = "text" name = "userName" id = "userName">
<input type = "password" name = "password" id = "password">
<input type = "submit">
</form>
</body>
</html>
通过jsp调用我的动作时出现错误:
输入状态报告
message /Login.spring
description请求的资源(/Login.spring)不可用。
然而,没有网址映射为&#34; .spring&#34;在jsp或controller或web.xml中
@onepotato
这是我的应用程序上下文
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost/springmvc"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceBean"></property>
<!--<property name="mappingResources">
<value>com.lnt.Pojo/Login.hbm.xml</value>
</property>-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplateBean" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactoryBean"></property>
</bean>
<!-- <bean id="AuthenticateServiceBean" class="com.lnt.services.AuthenticateServices">
<property name="hibernateTemplate" ref="hibernateTemplateBean"></property>
</bean> -->
</beans>
以下是我的MVC Dispatcher
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<context:component-scan base-package="com.lnt.controller"></context:component-scan>
<bean id = "viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
答案 0 :(得分:0)
你只需要输入&#34; /登录&#34;而不是&#34; .html&#34;在请求映射中。这应该有效,如果它没有尝试将请求映射注释放在方法之上而不是类。
答案 1 :(得分:0)
1.检查控制器是否通过从控制器打印控制台中的某些值来控制器。如果它打印,那么请求映射没有问题。
2.确保viewResolver bean下提供的jsp路径与服务器上jsp文件的路径匹配。
3.如果控制器没有到达控制器,则尝试从&#34; Login.htm&#34;更改@RequestMapping。到&#34; /Login.htm"因为我认为浏览器通常附加&#34; /&#34;在提交表格之前。
如果我遇到同样的问题,我会以上述方式解决。 :)
答案 2 :(得分:0)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring/servlet-context.xml,
classpath:Spring/security-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
<session-config>
<session-timeout>60</session-timeout>
</session-config>