Page 1 of 1

Some problems with new @Controller file

Posted: Wed Apr 25, 2012 4:05 am
by davidW
Sorry but I mistaken with the post....it must be in Getting Started forum


Good morning,

I have installed the project with the instructions of "Getting Started", I switched the database to a postgreSQL and changed some things for the look and feel.

The problem is that I am trying to make a new jsp page (I am really rookie in Spring) and it does not work. That is I did:

-Create a new Controller class in the package of CheckoutController.java at the project "site"
-Add to the applicationContext-servlet.xml this lines in "site-war" project (aboutUsController is my @Controller ):

Code: Select all

<context:component-scan base-package="org.broadleafcommerce">
         <context:include-filter type="regex" expression=".*web.*"/>
         <context:exclude-filter type="regex" expression=".*"/>
     </context:component-scan>

       <bean id="aboutUsController" class="com.webtiser.web.controller.AboutUsController">   
   </bean>
<property name="mappings">
         <props>
            <prop key="/store">catalogController</prop>
            <prop key="/basket/**">blCartController</prop>
            <prop key="/registerCustomer/*">blRegisterCustomerController</prop>
            <prop key="/checkout/*">mycompanyCheckoutController</prop>      
            <prop key="/aboutUs/*">aboutUsController</prop>      
         </props>
      </property>


-This is the code of AboutUsController (to test I used the current cart page):

Code: Select all

@Controller("aboutUsController")
@RequestMapping("/aboutUs")
public class AboutUsController {   

   @RequestMapping(method = RequestMethod.GET)
    public String checkout(@ModelAttribute CheckoutForm checkoutForm,
            BindingResult errors,
            ModelMap model,
            HttpServletRequest request) {
       
       return "redirect:/basket/currentCart.htm";
    }   

   @RequestMapping(method = RequestMethod.POST)
    public String aboutUs(@ModelAttribute CheckoutForm checkoutForm,
            BindingResult errors,
            ModelMap model,
            HttpServletRequest request) {
       
      return "redirect:/basket/currentCart.htm";
    }


In my jsp I write this:

Code: Select all

<a href="<c:url value="/aboutUs" />"> 



The first problem I had is that the ant task said to me that "it cant find the class file". I looked for it in the war file that I generated with ant and I didnt see it, I looked for it in the project "site-war" and I didnt see it.....I dont know why the java files arent in "site war" project, I found it in my "site" project
I copied it to resolve the problem (I hope you can say me what is my problem with that) and it works....but when I "clicked" in the link <a href="<c:url value="/aboutUs" />"> the message was :

HTTP ERROR 404
Problem accessing /mycompany/aboutUs. Reason:
NOT_FOUND



I hope you can help me , sorry for the huge post.... I think it would be better that you have all the information.

Best Regards

David

Re: Some problems with new @Controller file

Posted: Thu May 03, 2012 6:11 am
by davidW
Hello we solve the problem.

First, about the problem with .class files we solve it this way:
We copy the .class files from /site/target/classes/com to /site-war/target/mycompany/WEB-INF/classes/com


About the @controller we solve this way:

@Controller("tabController")
public class TabController {

Code: Select all

 

@Controller("tabController")
public class TabController {
 @RequestMapping(value = "/aboutUs", method = {RequestMethod.GET})
    public String aboutUs(@ModelAttribute Object o,
            BindingResult errors,
            ModelMap model,
            HttpServletRequest request) {

        return "cms/aboutUs";
    }

}



and the applicationContext-servlet.xml

Code: Select all

<bean id="urlMapping"
      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      <property name="order" value="1" />
      <property name="alwaysUseFullPath" value="true" />
      <!-- property name="interceptors">
         <list>
            <ref bean="catalogDisplayInterceptor" />
         </list>
      </property -->
       <property name="interceptors">
           <list>
               <ref bean="localeChangeInterceptor"/>
           </list>
       </property>
      <property name="mappings">
         <props>            
            <prop key="/aboutUs">tabController</prop>            
         </props>
      </property>
   </bean>



Bye

Re: Some problems with new @Controller file

Posted: Mon May 07, 2012 1:53 pm
by bpolster
David,

Glad you were able to get this working.

A couple of notes. The new Java class that you added in your "site" project would not be accessible in your site-war unless you did a "maven-build" of the project if you are using the default project structure.

Also, with Spring-MVC, you can use declarative mapping in XML like you've done in your working example. Alternatively, you can rely on the annotations (e.g. the @Controller and @RequestMapping) tags. To have Spring-MVC pick up these annotations, you would need to add you package to the "component-scan" entry.