2015-01-04 62 views
0

我想在我的HTML表单的提交操作上调用一个简单的弹簧控制器。 但是弹簧控制器没有被调用。我搜查allot,但没有理解缺失点。任何人都可以帮助解决这个问题。未从HTML表单调用弹簧控制器提交

下面是我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5"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_2_5.xsd"> 

<welcome-file-list> 
<welcome-file>index.jsf</welcome-file> 
</welcome-file-list> 
<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>/WEB-INF/spring-servlet.xml</param-value> 
</context-param> 
<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<listener> 
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 
<servlet> 
<servlet-name>Faces Servlet</servlet-name> 
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
<load-on-startup>0</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>Faces Servlet</servlet-name> 
<url-pattern>*.faces</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
<servlet-name>Faces Servlet</servlet-name> 
<url-pattern>*.jsf</url-pattern> 
</servlet-mapping> 
</web-app> 

和我为spring-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:mvc="http://www.springframework.org/schema/mvc" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd  http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd" xmlns:context="http://www.springframework.org/schema/context"> 

<mvc:annotation-driven /> 
<context:component-scan base-package="com.src.main"></context:component-scan> 
<context:annotation-config/> 
<mvc:resources location="/page/css/" mapping="/page/css/**"/> 
<mvc:resources location="/page/content/" mapping="/page/content/**"/> 
<!-- <mvc:resources location="/jsp/js/" mapping="/jsp/js/**"/>--> 
<bean id="loginBean" class="com.src.main.LoginBean" scope="request"> 
<aop:scoped-proxy/> 
</bean> 
<bean id="userLogin" class="com.src.main.UserLogin" scope="session"> 
<aop:scoped-proxy/> 
</bean> 
<bean id="handleApplicationInitProcessor" class="com.src.main.process.HandleApplicationInitProcessor"> 
<property name="userLogin" ref="userLogin"></property> 
<property name="loginBean" ref="loginBean"></property> 
</bean> 
</beans> 

和我的JSP是如下:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> 
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> 
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<f:subview id="user_login_subview"> 
<html> 
<head> 
<base href="<%=basePath%>"> 
<title>My JSP 'userLogin.jsp' starting page</title> 
<meta http-equiv="pragma" content="no-cache"> 
<meta http-equiv="cache-control" content="no-cache"> 
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
<meta http-equiv="description" content="This is my page"> 
<!-- 
<link rel="stylesheet" type="text/css" href="styles.css"> 
--> 
</head> 
<body> 
<form id="login_form" method="post" action="/processLogin"> 
<div id="login_div"> 
<h3 id="login_text" class="borderText">LOGIN</h3> 
<div id="user_name_div"> 
<h:outputText id="loging_label" value="Username :" /> 
<h:inputText id="login_field" size="30" value="#{loginBean.userName}" tabindex="1" styleClass="textBox"></h:inputText> 
</div> 
<div id="password_div"> 
<h:outputText id="password_label" value="Password :" /> 
<h:inputText id="password_field" size="30" value="#{loginBean.password}" tabindex="2" styleClass="textBox"></h:inputText> 
</div> 
<div id="remember_me_div"> 
<h:selectBooleanCheckbox id="remember_me_checkbox" value="#{loginBean.rememberMe}" tabindex="3" styleClass="checkbox"/> 
<h:outputText id="remember_me_label" value="Remember me" /> 
</div> 
<div id="action_buttons_div"> 
<input type="submit" id="submit_button" value="submit" class="button"/> 
<input type="reset" id="reset_button" value="reset" class="button"/> 
</div> 
</div> 
</form> 
</body> 
</html> 
</f:subview> 

和控制器UserLogin.java是如下,

package com.src.main; 
public class UserLogin { 

private String userName; 
private String password; 

public UserLogin(){ 

} 
public String getUserName() { 
return userName; 
} 
public void setUserName(String userName) { 
this.userName = userName; 
} 
public String getPassword() { 
return password; 
} 

public void setPassword(String password) { 
this.password = password; 
} 


} 

我的控制器的名字是:

package com.src.main.process; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import com.src.main.LoginBean; 
import com.src.main.UserLogin; 
@Controller 
public class HandleApplicationInitProcessor { 

private UserLogin userLogin; 
private LoginBean loginBean; 
public HandleApplicationInitProcessor() { 
} 

@RequestMapping(value = "/processLogin", method = RequestMethod.POST) 
public String process(){ 

this.getUserLogin().setUserName(this.getLoginBean().getUserName()); 
this.getUserLogin().setPassword(this.getLoginBean().getPassword()); 
System.err.println("UserName : "+this.getUserLogin().getUserName()); 
System.err.println("Password : "+this.getUserLogin().getPassword()); 


return "Hello World !"; 
} 
public UserLogin getUserLogin() { 
return userLogin; 
} 
public void setUserLogin(UserLogin userLogin) { 
this.userLogin = userLogin; 
} 
public LoginBean getLoginBean() { 
return loginBean; 
} 

public void setLoginBean(LoginBean loginBean) { 
this.loginBean = loginBean; 
} 
} 

最初我正在尝试使用JSF表单提交标签,但由于这个问题,我尝试了简单的HTML表单提交,但仍然是同样的问题。 我的控制台上也没有出现任何错误。

+0

您正混杂着JSF和Spring Web MVC代码。您还没有在web.xml中声明DispatcherServlet。请通过Spring Web MVC入门教程。 – shazin 2015-01-04 14:58:43

+0

我不想使用它。这就是为什么我添加了requestcontextListener。 – 2015-01-04 15:03:01

回答

0

我认为你应该做如下修改代码中的

1.Declare 的DispatcherServlet在web.xml文件,如下图所示。例如: -

 <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> 

2.从弹簧配置文件中删除下面的行。

<bean id="handleApplicationInitProcessor" class="com.src.main.process.HandleApplicationInitProcessor"> 
    <property name="userLogin" ref="userLogin"></property> 
    <property name="loginBean" ref="loginBean"></property> 
</bean> 

3.Just地方@Autowired你在哪里声明它(Controller class)之前。像下面

@Autowired 
private UserLogin userLogin; 
@Autowired 
private LoginBean loginBean; 

4.取出<context:annotation-config/>与您使用<mvc:annotation-driven />

+0

如果我使用此标记并执行上述更改,我的控制器会被调用。

但是Iam在我的bean中获取空值,我也尝试将范围从请求更改为会话以及仍然获得空值。你的控制器现在叫做 – 2015-01-04 15:29:08

+0

? – RE350 2015-01-04 15:30:02

+0

在下面的控制器中更改您的方法,传递参数@RequestBody UserLogin userLogin,其中包含您从JSP页面发送的值.. – RE350 2015-01-04 15:32:32