0

I have a basic problem. I am a beginner in Spring MVC Web framework. I want to call a method from my controller class when I click the button in my jsp file. I have tried all possible solution but I cannot call it yet. Thanks for your helps. Here is my code

/*Controller*/
        enter code here

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package controller;

    import rest.Userdata;
    import java.util.Map;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import service.UserService;

    /**
     *
     * @author simegra
     */
    @Controller
    public class UserController {

        @Autowired
        private UserService userService;

        @RequestMapping("/newUser")
        public String setupForm(Map<String, Object> map) {
            Userdata user = new Userdata();

            map.put("user", user);
            return "user";
        }

        @RequestMapping(value = "/success.htm", method = RequestMethod.POST)
        public String doActions(@ModelAttribute Userdata user, BindingResult result, @RequestParam String action, Map<String, Object> map) {
            //Userdata userResult=new Userdata();
            System.out.println("I AM CALLING");

             switch(action.toLowerCase()){
             case "signup":
             userService.add(user);
             userResult = user;
             break;
             case "edit":
             userService.edit(user);
             userResult = user;
             break;
             case "delete":
             userService.delete(user.getId());
             userResult = new Userdata();
             break;
             case "search":
             Userdata searchedUser = userService.getUser(user.getId());
             userResult = searchedUser!=null? searchedUser : new Userdata();
             break;
             }
             map.put("user", userResult);

            return "success";

        }
    }

/*My JSP*/

<%-- 
    Document   : newUser
    Created on : 15.Ağu.2013, 10:07:28
    Author     : toshiba
--%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Sing Up</title>
     <style type="text/css">
    .container {
        width: 500px;
        clear: both;
    }
    .container input {
        width: 50%;
        clear: both;
    }


    div.row {
  clear: both;
  padding-top: 10px;
  }div.row span.label {
  float: left;
  width: 100px;
  text-align: right;
  }div.row span.formw {
  float: right;
  width: 335px;
  text-align: left;
  } 

    </style>

</head>




<script language="javascript" type="text/javascript">


function isValidate(){
  if (document.forms["signup"]["fname"].value=="" || document.forms["signup"]["fname"].value==null ) {       
       document.getElementById("divfname").innerHTML = " <font color= red >* Please Enter Your First Name </font>";

            return false;
  }else{document.getElementById("divfname").innerHTML =""}

  if ( document.forms["signup"]["lname"].value=="" ||document.forms["signup"]["lname"].value==null ) {       
        document.getElementById("divlname").innerHTML = "<font color= red >* Please Enter Your Last Name</font>";
    return false;
  }else{document.getElementById("divlname").innerHTML =""}

  if ( document.forms["signup"]["mail"].value=="" ||document.forms["signup"]["mail"].value==null) {       
        document.getElementById("divemail").innerHTML = "<font color= red >* Please Enter Your Email Address</font>";
    return false;
  }else{document.getElementById("divemail").innerHTML =""}


        if(document.forms["signup"]["m"].checked==false && document.forms["signup"]["f"].checked==false){
        document.getElementById("radioB").innerHTML = "<font color= red >* Choose Your Gender</font>";
    return false;
      } else{document.getElementById("radioB").innerHTML =""}


  var x = document.forms["signup"]["bdate"].value;
    var reg = /(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d/;

        if (!x.match(reg)) {
         document.getElementById("divbdate").innerHTML = "<font color= red >* Please Enter a valid Birth Date</font>";
      return false;
    }else { document.getElementById("divbdate").innerHTML = "";
    }    

    var ph =/[0-9]|\./;
    if ( !document.forms["signup"]["phone"].value.match(ph) ||document.forms["signup"]["phone"].value=="" || document.forms["signup"]["phone"].value==null ) {       
        document.getElementById("divphone").innerHTML = "<font color= red >* Please Enter Phone Number(use only 0-9)</font>";
    return false;}else{document.getElementById("divphone").innerHTML = "";}

  var passw=  /^[A-Za-z]\w{7,14}$/;   
     if(!document.forms["signup"]["pass1"].value.match(passw) ||document.forms["signup"]["pass1"].value=="" || document.forms["signup"]["pass1"].value==null)   
{    document.getElementById("divpass1").innerHTML = "<font color= red >* Please Enter Password[7 to 15 characters which contain at least one numeric digit and a special character] </font>"; 
         return false;
    }else{document.getElementById("divpass1").innerHTML = "";}

   // var pw1=document.forms["signup"]["pass1"].value;
    // var pw2=document.forms["signup"]["pass2"].value;

     if(!document.forms["signup"]["pass1"].value!=document.forms["signup"]["pass2"].value) {   
         document.getElementById("divpass2").innerHTML = "<font color= red >* Match is not correct";
         return false;
    }else{document.getElementById("divpass2").innerHTML = "";}

    return true;
}
</script>
<body>

    <center>  <p><b><font size="6">SIGN UP</font></b></p> </center>

    <br>
    <div style="width: 350px; background-color: activeborder ;
        border: 1px dotted #333; padding: 5px;
        margin: 0px auto";>

        <form name="input" action="success.htm"   method="POST" id="signup"   >
              <table>
            <fieldset style ="background-color: aliceblue;">
      <legend>Personal Information:</legend>
        <div class="row">
           <span class="label"> First name:</span><input path="firstname" type="text" name="firstname" id ="fname" >
            <div id="divfname" ></div> 

        </div>
      <div class="row">
           <span class="label">  Last name:</span><input path="lastname"type="text" name="lastname" id="lname">
            <div id="divlname" ></div> 

             </div>
            <div class="row">
            <span class="label"> Email:</span><input path="email" type="text" name="email" id="mail">
            <div id="divemail" ></div>

             </div>

      <div class="row">  
                   <div >
                       <dd> <input type="radio" path="gender" name="sex" value="male" id="m">Male   </dd>            
            </div>
             <div class="row">
                       <dd><input type="radio" path="gender" name="sex" value="female" id="f" >Female </dd>
            <div id="radioB" ></div>                                                 
             </div></div>  

            <div class="row">
                <span class="label">  Date of Birth: </span> <input path="firstname" type="text" name="birthday" id="bdate">
            <div id="divbdate" ></div>                                                 
            </div> 
            <div class="row">
          <span class="label">   Phone     : </span><input path="phone" type="text" name="email"id="phone">
            <div id="divphone" ></div>                                                 
           </div>
            <div class="row">
           <span class="label">  Password:</span> <input type="password" name="pwd" id="pass1">
            <div id="divpass1" ></div>                                                 
             </div>
          <div class="row">
           <span class="label">  Re-enter:</span> <input type="password" name="pwd2" id="pass2">
            <div id="divpass2" ></div>                                                 
             </div>
      <center>
             <input type="submit"  name="action" value="signup" >
      </center>

  </fieldset>
        </table>
        </form>

    </div>
    </div>
</body>
</html>

/*Dispatcher-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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    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="homePage.htm">homeController</prop>
                <prop key="newUser.htm">userController</prop>

                 <prop key="success.htm">sucController</prop>

            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="homeController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="homePage" />
    <bean name="userController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="newUser" />
    <bean name="sucController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="success" />





    <bean id="mydataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://tmnt.zapto.org:5432/spr"/>
    <property name="username" value="smg"/>
    <property name="password" value="S1m3gra"/>
    </bean> 

    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="mydataSource"/>
        <property name="packagesToScan" value="data"/> 
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>

        <property name="hibernateProperties">
     <props>

    <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
    <prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop>
    <prop key="hibernate.connection.url">jdbc:postgresql://tmnt.zapto.org:5432/spr</prop>
    <prop key="hibernate.connection.username">smg</prop>
    <prop key="hibernate.connection.password">S1m3gra</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.current_session_context_class">thread</prop>
    <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
        </props>
        </property>
    </bean>


    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>
4
  • pls share code snippet here Commented Aug 24, 2013 at 13:56
  • @aykut, when posting code only post those portions that are relevant to the problem. Commented Aug 24, 2013 at 14:15
  • Please separate different kind of code in different portions Commented Aug 24, 2013 at 16:41
  • For what urls is the dispatcher servlet configured to handle? Commented Aug 24, 2013 at 16:57

2 Answers 2

1

I think using "/user/success.htm" as your action URL for your "input" form would do the work as you configured ControllerClassNameHandlerMapping as your controller mapping handler.

Because when you are putting "/success.htm" it is actually getting mapped with one of your ParameterizableViewController, not with UserController's method (with /success.htm as the url mapping). Assuming that your DispatcherServlet is mapped with "/".

Sign up to request clarification or add additional context in comments.

Comments

0

I didn't see any use of spring:url in your JSP. It is the classic way of composing url with Spring MVC, because it will automatically add the base url to your application. You didn't mention any mapping to your controller so it will be reached throw root url '/', and if you want to reach your method 'doActions()' you have to use :

<spring:url value="/success.htm" var="success"/>

and then :

<a href="${success}>toSuccess</a>

Also, you should use JSPX, not JSP page, it is the recommend way with Spring MVC.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.