SimpleUrlHandlerMapping不与控制器映射(CONTROL不是控制器)

时间:2015-09-08 06:30:53

标签: java xml jsp

这里我使用SimpleUrlHandlerMapping的urlMap属性来映射控制器,但它没有映射到Controller.Here我已经把调度程序,Jsp和控制器的代码。

我已经包含以下Jar文件 - :

  com.springsource.org.apache.commons.fileupload-1.2.0.jar
  com.springsource.org.apache.commons.httpclient-3.1.0.jar
  com.springsource.org.apache.commons.logging-1.1.1.jar
  com.springsource.org.apache.log4j-1.2.15.jar
  com.springsource.org.codehaus.jackson.mapper-1.0.0.jar
  jmxtools-1.2.1.jar
  jstl-1.2 (1).jar
  org.springframework.asm-3.0.1.RELEASE-A.jar
  org.springframework.beans-3.0.1.RELEASE-A.jar
  org.springframework.context-3.0.1.RELEASE-A.jar
  org.springframework.core-3.0.1.RELEASE-A.jar
  org.springframework.expression-3.0.1.RELEASE-A.jar
  org.springframework.oxm-3.0.1.RELEASE-A.jar
  org.springframework.web-3.0.1.RELEASE-A.jar
  org.springframework.web.portlet-3.0.1.RELEASE-A.jar
  org.springframework.web.servlet-3.0.1.RELEASE-A.jar
  org.springframework.web.struts-3.0.1.RELEASE-A.jar
  spring-webmvc-3.0.5.RELEASE.jar

调度-servlet.xml中:

 
        <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            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.0.xsd 
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">



        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix">
                        <value>/WEB-INF/JSPpages/</value>
              </property>
              <property name="suffix">
                        <value>.jsp</value>
              </property>
        </bean>


        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                     <property name="urlMap">
                               <map>
                                    <entry key="/Registration.html">           
                                    <ref bean="RegistrationCon" />                                  
                                    </entry>
                               </map>
                     </property>
           </bean>
           <bean id="RegistrationCon" class="controllers.registrationController.RegistrationController">      


                <property name="commandName">
                          <value>RegistrationBean</value>
                </property>
                <property name="commandClass">
                          <value>formBeans.registrationBean.RegistrationBean</value>
                </property>

                <property name="sessionForm">
                          <value>false</value>
                </property>

                <property name="formView">
                          <value>Registration</value>     
                </property>
                <property name="successView">
                          <value>RegistrationSuccess</value>
                </property>

            </bean>
        </beans>

这个RegistrationController:

package controllers.registrationController;

import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import formBeans.registrationBean.RegistrationBean;

@SuppressWarnings("deprecation")
public class RegistrationController extends SimpleFormController  {                                                                                        

    protected ModelAndView onSubmit(HttpServletRequest req,HttpServletResponse res,Object command)throws ServletException  //--OnSubmit Method Starts--//
    {  

        System.out.println("In controller");

        RegistrationBean regBean = (RegistrationBean) command;  

        String loginName=regBean.getLoginId();
        System.out.println("Name---->"+loginName);


        String pwd=req.getParameter("pwd");
        System.out.println("PassWord---->"+pwd);

        ArrayList<String> al=new ArrayList<String>();
        al.add(loginName);
        al.add(pwd);

        ModelAndView mav=new ModelAndView("/RegistrationSuccess");
        mav.addObject("ArrayList",al);
        mav.addObject("regBean",regBean);
        return mav; 

    }


}

注册(JSP):

     <%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %>
      <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
     <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head><title>User Personal Details</title>
<style>
table, th{
    border: 1px solid black;
}
label{
  font-family: "Trebuchet MS", Verdana, Halvetica, Arial;
  font-size: 12px;
  color: blue;
}

.textfield {

  width: 250px;
  border: 1px solid #AF9D72;
  background-color: #F2ECD7;
}
</style></head>
<body bgcolor="#DDDDDD">
 <h3>User Registration</h3>
<br/>
<form:form commandName="RegistrationBean" method="POST" name="RegistrationBean">
<div align="center" style="width:100%; height:100%">
<table style="height:250px; width:400px">
<tr>
    <th>Heading</th>
    <th>Input</th>
  </tr>
<tr>
<td align="left"><label>Name:</label></td>
<td><form:input path="loginId" class="textfield"/></td>
</tr>
<tr>
<td align="left"><label>Password:</label></td>
<td><form:input path="pwd" class="textfield"/></td>
</tr>
<tr>
<td align="left"><label>Confirm Password:</label></td>
<td><form:input path="cpwd" class="textfield"/></td>
</tr>
<tr> 
<td colspan="2" align="center"><input type="submit" value="Save"/></td> 
</tr>
</table>
</div>
</form:form>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

试试这个:

 <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
           <property name="urlMap">
            <props>
               <prop key="/Registration.html">RegistrationCon</prop>
             </props>
           </property>
        </bean>

如果URL查找应始终使用当前servlet上下文中的完整路径,则可能需要设置:

<property name = "alwaysUseFullPath" value = "true" />

到urlMapping bean

稍后编辑: 将此用于 urlMap ,上面的代码用于映射,抱歉。

<property name="urlMap">
   <map>
       <entry key="/Registration.html" value-ref="RegistrationCon"/>
   </map>
  ...

答案 1 :(得分:0)

问题已解决:

我使用的Jar文件 - :

aopalliance-1.0.jar
commons-logging-api-1.1.1.jar
jstl.jar
org.springframework.asm-3.0.4.RELEASE.jar
org.springframework.beans-3.0.4.RELEASE.jar
org.springframework.context-3.0.4.RELEASE.jar
org.springframework.context.support-3.0.4.RELEASE.jar
org.springframework.core-3.0.4.RELEASE.jar
org.springframework.expression-3.0.4.RELEASE.jar
org.springframework.web-3.0.4.RELEASE.jar
org.springframework.web.servlet-3.0.4.RELEASE.jar
standard.jar

的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/javaee" 
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>LogOutSystemChecking</display-name>


  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>


  <servlet>
          <servlet-name>dispatcher</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <load-on-startup>1</load-on-startup>
  </servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

调度-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.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


                                   <!-- View Resolver Starts -->

 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix">
                        <value>/WEB-INF/jspViews/</value>
              </property>
              <property name="suffix">
                        <value>.jsp</value>
              </property>
        </bean>
                                  <!-- View Resolver Starts -->

        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">


                     <property name="urlMap">
                               <map>
                                    <entry key="/UserRegistration.html">           
                                    <ref bean="RegCon" />                                     <!-- Controller-1 Mapping -->                     
                                    </entry>
                               </map>
                     </property>
           </bean>
           <bean id="RegCon" class="controllers.registerController.RegistrationController">   <!-- Controller-1 Mapping -->   


                <property name="commandName">
                          <value>registration</value>                                        <!--form:form commandName="registration" method="post" In UserRegistration.JSP-->
                </property>
                <property name="commandClass">
                          <value>formBeans.registerBean.RegistrationBean</value>
                </property>

                <property name="sessionForm">
                          <value>false</value>
                </property>

                <property name="formView">
                          <value>UserRegistration</value>     
                </property>
                <property name="successView">
                          <value>UserRegistrationSuccess</value>
                </property>

                <property name="validator">
                          <bean class="validatorsPackage.registrationValidator.RegistrationValidations"/> <!-- Validate Page If user Does't enter Username,Password,CompanyAddress -->
                </property>
            </bean>

            <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">  <!-- To show Message If error in a jsp page like Plz enter Username -->
                  <property name="basename" value="message" />
            </bean>


</beans>

RegistrationBean.java - :

package formBeans.registerBean;

public class RegistrationBean { 

    //--global variable declaration Starts--//
    private String name;
    private String comAdd;
    private String pwd;
    private String cpwd;
    private char   gender;
    private int    empNumber;
    //--global variable declaration Ends--//

    //--Getter & Setter Starts--//
    public String getComAdd() {
        return comAdd;
    }


    public void setComAdd(String comAdd) {
        this.comAdd = comAdd;
    }


    public String getPwd() {
        return pwd;
    }


    public void setPwd(String pwd) {
        this.pwd = pwd;
    }


    public String getCpwd() {
        return cpwd;
    }


    public void setCpwd(String cpwd) {
        this.cpwd = cpwd;
    }


    public char getGender() {
        return gender;
    }


    public void setGender(char gender) {
        this.gender = gender;
    }


    public int getEmpNumber() {
        return empNumber;
    }


    public void setEmpNumber(int empNumber) {
        this.empNumber = empNumber;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
        System.out.println("Name--:"+name);
    }
    //--Getter & Setter Ends--//

    public RegistrationBean() 
    {
        // TODO Auto-generated constructor stub
    }

} //--Class Ends--//

RegistrationController.java - :

package controllers.registerController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import formBeans.registerBean.RegistrationBean;

public class RegistrationController extends SimpleFormController {     //-- Class Starts--//

     protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception //--onSubmit() Method Starts--//
     {
            System.out.println("into the doSubmitAction() method.......");

            RegistrationBean regBean = (RegistrationBean) command;
            System.out.println("studentBea"+regBean.getName());
            ModelAndView mv = new ModelAndView(this.getSuccessView());
            mv.addObject("regBean", regBean);
            return mv;
     }//--onSubmit() Method Starts--//

    public RegistrationController() 
    {
        // TODO Auto-generated constructor stub
    }

}  //-- Class Ends --//

UserRegistration.jsp - :

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1" isELIgnored="FALSE"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>                                 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>     
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>              <!-- error remove after add Standard.jar -->

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<style>
table, th{
    border: 1px solid black;
}
label{
  font-family: "Trebuchet MS", Verdana, Halvetica, Arial;
  font-size: 12px;
  color: blue;
}

.textfield {

  width: 250px;
  border: 1px solid #AF9D72;
  background-color: #F2ECD7;
}

.error {
color: #ff0000;
font-style: italic;
}
.errorblock{
color: #ff0000;
background-color: black;
width:400px;
text-align: center;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SignUp</title>
</head>
<body bgcolor="#DDDDDD">
<form:form commandName="registration" method="post">
<div align="center" style="width:100%; height:100%">     <!-- Div tag ka data center me(meaning of aling) -->
<form:errors path="*" cssClass="errorblock" element="div"/>
 <table style="height:250px; width:400px">
 <tr>
    <th>Heading</th>
    <th>Input</th>
  </tr>
    <tr>
          <td align="left"><label>Employee Name:</label></td>
          <!--<td>Name:</td>-->
        <td align="left"><form:input path="name" class="textfield"/></td>         <!-- Insted of NAME Attribute we use PATH in Spring  -->
        <td><form:errors path="name" cssClass="error"/></td>
    </tr>

    <tr>
          <td align="left"><label>Employee Number:</label></td>
          <!--<td>Name:</td>-->
          <td align="left"><form:input path="empNumber" class="textfield"/></td>  <!-- Insted of NAME Attribute we use PATH in Spring  -->
          <td><form:errors path="empNumber" cssClass="error"/></td>
    </tr>

    <tr>
          <td align="left"><label>Company Address:</label></td>
          <!--<td>Name:</td>-->
          <td align="left"><form:textarea path="comAdd" class="textfield"/></td>  <!-- Insted of NAME Attribute we use PATH in Spring  -->
           <td><form:errors path="comAdd" cssClass="error"/></td>
    </tr>
    <tr>
        <td align="left"><label>Gender:</label></td>
        <td align="left">
            <form:radiobutton path="gender" value="M" label="M" /> 
            <form:radiobutton path="gender" value="F" label="F" />
        </td>
         <td><form:errors path="gender" cssClass="error"/></td>
    </tr>

    <tr>
       <td align="left"><label>Password:</label></td>
        <!--<td>Password:</td>-->
        <td align="left"><form:password path="pwd" class="textfield"/></td>
         <td><form:errors path="pwd" cssClass="error"/></td>
    </tr>
     <tr>
       <td align="left"><label>Confirm Password:</label></td>
        <!--<td>Password:</td>-->
        <td align="left"><form:password path="cpwd" class="textfield"/></td>
    </tr>

    <tr>
        <td colspan="2" align="center">
            <input type="submit" value="Rigester/Save"/>
        </td>
    </tr>
</table>  
</div>
</form:form>
</body>
</html>