我是Spring MVC的新手,我必须从头创建一个项目,我决定使用Spring MVC,但是在servlet-dispatcher.xml中映射控制器以及从视图中访问方法时遇到一些问题
我的项目结构如下:
我的web.xml:
<web-app version="3.0"
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-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- "Dispatcher" servlet de Spring -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup> <!-- 1 = cuando una peticion sea enviada, Spring sera el primero en atenderla -->
</servlet>
<servlet-mapping> <!-- Se mapea que tipo de peticiones va a atender el servlet de Spring -->
<servlet-name>dispatcher</servlet-name> <!-- Mismo nombre con que se dio de alta -->
<url-pattern>*.htm</url-pattern> <!-- Para atender todas las peticiones seria / -->
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<!-- Archivo de Bienvenida -->
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
我的dispatcher-servlet.xml
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:simple="http://cxf.apache.org/simple"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://cxf.apache.org/simple
http://cxf.apache.org/schemas/simple.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Mapear URL's de cada controller -->
<!-- Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it. -->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="login.htm">loginController</prop>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<!-- ViewResolver: Ubicacion de las vistas que Spring regresará al response -->
<bean id="viewResolver" p:prefix="/WEB-INF/pages/" p:suffix=".jsp" class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
<!-- El controller para la vista inicial -->
<bean name="loginController" p:viewName="login" class="org.springframework.web.servlet.mvc.ParameterizableViewController" />
<!-- El resto de los Controllers -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="indexController" class="com.controller.IndexController" />
<!-- INTERNACIONALIZACION -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages" /> <!-- Ubucacion de los archivos .properties con los labels -->
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" /> <!-- Idioma por Default -->
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="locale" /> <!-- Variable para interceptar el cambio de idioma -->
</bean>
</mvc:interceptors>
</beans>
我的控制器[LoginController.java]
package com.controller;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LoginController {
@RequestMapping(value = "validar")
public ModelAndView login(){
System.out.println("Entro al controller de Login");
ModelAndView model = new ModelAndView();
model.setViewName("index"); //Go to Index after validating the user
return model;
}
}
我的Login.jsp
<%@ taglib uri="http://www.springframework.org/tags" prefix="intl"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Creative - Bootstrap 3 Responsive Admin Template">
<meta name="author" content="GeeksLabs">
<meta name="keyword" content="Creative, Dashboard, Admin, Template, Theme, Bootstrap, Responsive, Retina, Minimal">
<link rel="shortcut icon" href="img/favicon.ico">
<!-- Bootstrap CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- bootstrap theme -->
<link href="css/bootstrap-theme.css" rel="stylesheet">
<!--external css-->
<!-- font icon -->
<link href="css/elegant-icons-style.css" rel="stylesheet" />
<link href="css/font-awesome.css" rel="stylesheet" />
<!-- Custom styles -->
<link href="css/style.css" rel="stylesheet">
<link href="css/style-responsive.css" rel="stylesheet" />
<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
<!-- HTML5 shim and Respond.js IE8 support of HTML5 -->
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->
<title>Admin Console</title>
</head>
<body class="login-img3-body" onload="callMe()">
<div class="container">
<form class="login-form" id="frmLogin" method="POST">
<div class="login-wrap">
<p class="login-img">
<h3 align="center"><intl:message code="label.appTitle" /></h3>
<img width="100%" src="img/logo_infoVialDF.png">
</p>
<div class="input-group">
<span class="input-group-addon"><i class="icon_profile"></i></span>
<input type="text" class="form-control" placeholder="<intl:message code="label.username" />" autofocus>
</div>
<div class="input-group">
<span class="input-group-addon"><i class="icon_key_alt"></i></span>
<input type="password" class="form-control" placeholder="<intl:message code="label.password" />">
</div>
<label class="checkbox">
<input type="checkbox" value="remember-me"><intl:message code="label.rememberMe" />
<span class="pull-right">
<select id="lang" onchange="onChange()">
<option value="es">Español</option>
<option value="en">Ingles</option>
</select>
<input type="hidden" id="lang"/>
</span>
</label>
<button class="btn btn-primary btn-lg btn-block" type="submit"><intl:message code="label.loginText" /></button>
</div>
</form>
</div>
</body>
</html>
<script type="text/javascript">
$(document).ready(function () {
$('#frmLogin').submit(function(event){
$.ajax({
url: 'validar',
type: 'POST',
success: function(response){
alert("Exito: "+response);
},
error: function (response) {
alert("Error: "+response);
}
});
return false;
});
});
function onChange() {
var e = document.getElementById("lang").value;
window.location.href = "?locale="+e;
e = e.options[e.selectedIndex].value;
}
function callMe(){
var locale = "${pageContext.response.locale}";
if(locale=="en")
document.getElementById("lang").value="en";
else if(locale=="es")
document.getElementById("lang").value="es";
}
</script>
我看一下,当你启动应用程序时,会加载Login.jsp视图(这项工作)并按下Login LoginController,然后转到index.jsp,它是来自应用程序的Dashboard视图。
我的错误是我无法访问LoginController和方法&#34;输入&#34;。 你能帮我吗?
答案 0 :(得分:0)
您遗失了action
<form class="login-form" id="frmLogin" method="POST">
需要像:
<form class="login-form" id="frmLogin" action="validar" method="POST">