Announcement Announcement Module
Collapse
No announcement yet.
URL read as path variable in controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • URL read as path variable in controller

    My showSpitterProfile method is being run instead of createSpitterProfile when I click on the "Register" link in my home.jsp file.

    What happens is instead of reading the url as spitters/edit. It reads "edit" as the username path variable. I have no clue why. But I have pinpointed it down to that.

    I have been stuck on this for about a day and a half. =\

    Any help would be appreciated!


    SpitterController:
    Code:
    @Controller
    @RequestMapping(value = "/spitters")
    public class SpitterController {
    
        private final SpitterService spitterService;
    
        @Inject
        public SpitterController(SpitterService spitterService){
            this.spitterService = spitterService;
        }
    
        @RequestMapping(value = "/edit", method = RequestMethod.GET, params="new")
        public String createSpitterProfile(Model model){
            model.addAttribute(new Spitter());
            return "spitters/edit";
        }
    
        @RequestMapping(value = "/register", method = RequestMethod.POST)
        public String addSpitterFromForm(@Valid Spitter spitter, BindingResult bindingResult){
            if (bindingResult.hasErrors())
                return "spitters/edit";
    
            spitterService.saveSpitter(spitter);
            return "redirect:/spitters/" + spitter.getUserName();
        }
    
        @RequestMapping(value="/{username}", method = RequestMethod.GET)
        public String showSpitterProfile(@PathVariable String username, Model model){
            model.addAttribute("username", spitterService.getSpitter(username));
            model.addAttribute("spittle", spitterService.getSpittlesForSpitter(username));
            return "spitters/viewSpitterProfile";
        }
    }

    Home.jsp:
    HTML Code:
    <html>
    <head>
        <title>Home - Spitter</title>
        <security:authorize access="isAuthenticated()">
            <h2>Hello <security:authentication property="principal.username"/>!</h2>
            <a href="/static/j_spring_security_logout">Logout</a>
        </security:authorize>
    
        <security:authorize access="! isAuthenticated()">
            <h2>Hello Guest!</h2>
            <s:url value="/spitters/edit" var="edit_url"/>
            <a href="${edit_url}">Register</a>
        </security:authorize>
    
    </head>
    <body>
    
    <div>
        <security:authorize access="hasRole('ROLE_SPITTER')">
            <s:url value="/home" var="spittle_url"/>
            <sf:form modelAttribute="spittle" action="${spittle_url}">
                <sf:label path="text"> <s:message text="Enter spittle: " /></sf:label>
                <sf:textarea path="text" rows="2" cols="40" />
                <sf:errors path="text" />
                <br>
                <div class="spitItSubmitIt">
                    <input type="submit" value="Spit it!" class="status-btn round-btn disabled" />
                </div>
            </sf:form>
        </security:authorize>
    </div>
    
    <div style="float:left">
        <h3>Lets see what the recent spits are: </h3>
        <ol class="spittle-list">
            <c:forEach var="spittle" items="${spittle}">
                <s:url value="/spitters/{spitterName}" var="spitter_url">
                    <s:param name="spitterName" value="${spittle.spitterUserName}"/>
                </s:url>
    
                <li>
                <span class="spittleListText">
                    <c:out value="${spittle.spitterUserName}"/>
                    - <c:out value="${spittle.text}"/> <br>
                    <small> <c:out value="${spittle.date}" /></small>
                </span>
                </li>
            </c:forEach>
        </ol>
    </div>
    
    </body>
    </html>

    Dispatcher:
    HTML Code:
        <context:component-scan base-package="com.springapp.mvc"/>
    
        <security:global-method-security secured-annotations="enabled" />
    
        <mvc:annotation-driven />
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
    
        <util:properties id="jdbcProperties" location="jdbc.properties" />
    
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
              p:properties-ref="jdbcProperties" />
    
        <bean id="spitterRepository" class="com.springapp.mvc.repository.SpitterRepositoryImpl" />
    
        <bean id="spittleRepository" class="com.springapp.mvc.repository.SpittleRepositoryImpl" />
    
        <bean id="spitterService" class="com.springapp.mvc.service.SpitterServiceImpl" />
    
        <bean class= "org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>
    
        <bean class= "org.springframework.web.servlet.view.tiles2.TilesConfigurer">
            <property name="definitions">
                <list>
                    <value>/WEB-INF/views/views.xml</value>
                </list>
            </property>
        </bean>
    
    
        <!-- Data Source -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
              p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
    
        <!-- H2 Database-->
        <jdbc:initialize-database ignore-failures="DROPS">
            <jdbc:script location="schema.sql" />
        </jdbc:initialize-database>
    
    
        <!-- JPA EntityManagerFactory -->
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
              p:dataSource-ref="dataSource" p:jpaProperties-ref="jdbcProperties">
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:database="${jpa.database}"
                      p:showSql="${jpa.showSql}"/>
            </property>
        </bean>
    
        <!--Transaction manager for a single JPA EntityManagerFactory-->
        <bean id="transactionManager"
              class="org.springframework.orm.jpa.JpaTransactionManager"
              p:entityManagerFactory-ref="entityManagerFactory" />
    
    </beans>
    Spring-security.xml:
    HTML Code:
        <jdbc-user-service id="userService" data-source-ref="dataSource"
                           users-by-username-query=
                                   "SELECT username, password, TRUE FROM SPITTER WHERE username=?"
                           authorities-by-username-query=
                                   "SELECT username, 'ROLE_SPITTER' FROM SPITTER WHERE username=?"/>
    
        <authentication-manager alias="authenticationManager">
            <authentication-provider user-service-ref="userService"/>
        </authentication-manager>
    
    </beans:beans>
    Web.xml
    HTML Code:
    <web-app version="2.4"
    	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    	<display-name>Spitter Application</display-name>
    
    
        <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>/</url-pattern>
    	</servlet-mapping>
    
        <welcome-file-list>
            <welcome-file> home.jsp </welcome-file>
        </welcome-file-list>
    
       <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/mvc-dispatcher-servlet.xml
                /WEB-INF/spring-security.xml
            </param-value>
        </context-param>
    
        <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>
    
    </web-app>

  • #2
    Bump. Can anyone help? Need more information? Liek my other controllers or anything?

    Comment


    • #3
      I suggest you take a look at your mappings...

      Code:
      @RequestMapping(value = "/edit", method = RequestMethod.GET, params="new")
      @RequestMapping(value="/{username}", method = RequestMethod.GET)
      No the URL that is hit is '/spitters/edit'... To what will it match...

      Comment


      • #4
        Originally posted by Marten Deinum View Post
        I suggest you take a look at your mappings...

        Code:
        @RequestMapping(value = "/edit", method = RequestMethod.GET, params="new")
        @RequestMapping(value="/{username}", method = RequestMethod.GET)
        No the URL that is hit is '/spitters/edit'... To what will it match...
        Wow... It was that simple... Thank you so much. I spent way too much time trying to figure thatout.

        Comment


        • #5
          I have a new problem.

          My form isn't binding properly to the object.

          Controller:
          Code:
          package com.springapp.mvc.controller;
          
          import com.springapp.mvc.model.Spitter;
          import com.springapp.mvc.service.SpitterService;
          import org.springframework.stereotype.Controller;
          import org.springframework.transaction.annotation.Transactional;
          import org.springframework.ui.Model;
          import org.springframework.validation.BindingResult;
          import org.springframework.web.bind.annotation.PathVariable;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.bind.annotation.RequestMethod;
          import org.springframework.web.bind.annotation.SessionAttributes;
          
          import javax.inject.Inject;
          import javax.validation.Valid;
          
          @Transactional
          @Controller
          @RequestMapping(value = "/spitters")
          public class SpitterController {
          
              private final SpitterService spitterService;
          
              @Inject
              public SpitterController(SpitterService spitterService){
                  this.spitterService = spitterService;
              }
          
              @RequestMapping(value = "/edit", method = RequestMethod.GET)
              public String createSpitterProfile(Model model){
                  model.addAttribute("spitter", new Spitter());
                  return "spitters/edit";
              }
          
              @RequestMapping(value = "/edit", method = RequestMethod.POST)
              public String addSpitterFromForm(@Valid Spitter spitter, BindingResult bindingResult, Model model){
                  if (bindingResult.hasErrors())
                      return "spitters/edit";
          
                  spitterService.saveSpitter(spitter);
                  return "redirect:/spitters/" + spitter.getUserName();
              }
          
              @RequestMapping(value="/{username}", method = RequestMethod.GET)
              public String showSpitterProfile(@PathVariable String username, Model model){
                  model.addAttribute("username", spitterService.getSpitter(username));
                  model.addAttribute("spittle", spitterService.getSpittlesForSpitter(username));
                  return "spitters/viewSpitterProfile";
              }
          }
          To my edit.jsp form. In debug mode I get null for my spitter object.

          Comment

          Working...
          X