开发者

How to know the moment when @RequestMapping fires?

I have a Spring MVC Controller class (bean):

@Controller
@RequestMapping("/index.jsp")
public class EjbCaller {

    @Autowired
    private InfoBean infoBean;

    public EjbCaller() {
        System.out.println("creating !!!!!!!!!!!!!!!!!!!!!!!!!!");
    }

    @ModelAttribute("textFromService")
    public String call() {
       System.out.println("!!!!!!!!!!!!!!!!!!!1 gogogogog");
       return infoBean.getRefSampleService().doService();
    }    
}

How to know that @RequestMapping("/index.jsp") fires well when I go to the index.jsp? Because i do not know if I'm putting right value to the @RequestMapping annotation, or maybe something wrong with @ModelAttribute because it does not fire as well..

In my index.jsp i have code like this:

<p>
   开发者_C百科 <span>from SampleService: ${textFromService} </span>
</p>

About my usage/settgins:

I have DispatcherServlet in web.xml, i have , bit it does not work. I guess ModelAndView this is old approach to use MVC, @ModelAttribute this is a new approach as i understand. So that's why i use @ModelAtrribute.

I have output in the jbossConsole from EJBCaller from constructor but not when call()-method is calling that's why i do not know if this method runs or not.


Controllers are just one part of the MVC equation, you should have:

Controllers with @RequestMapping annotations noting which URLs they handle, they (essentially) return views. In Spring MVC, these are done with ViewResolvers, the simplest is:

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

So then you can do something like

@RequestMapping(value="/test/{myParam}", method=RequestMethod.GET)
public ModelAndView myMethod(@PathVariable("myParam") String param) { 
     ModelAndView mv = new ModelAndView();
     mv.setViewName("index"); // now put index.jsp in /WEB-INF/views
     // try passing the input back to the view so you can play around
     // with the view/parameter handling
     mv.addObject("variableName", param); 
}

In your Spring config file, there are a bunch of options, I often do:

<mvc:annotation-driven />
<bean name="someController" class="..."/>

This will then get picked up.

Don't forget your org.springframework.web.servlet.DispatcherServlet in web.xml


I'm just beginnig with Spring framework and I use a well-tested sleight:

public void anyMethod() {
    throw new Error("You're here");
}
It is important to know, that there is no console output in J2EE applications, because these applications runs in container somewhere on server. (It is even a case of servlet in Tomcat.) You should use some logging system instead the STDOUT or throw an error (but - of course - not in release version in contradistinction to logging system.)

Why Error? As you might now, there are two kinds of throwables. Errors and exceptions extending RunTimeException you needn't to catch. Thus these throwables you can easily send out to cruise throught stack upwards and let them be caught and logged by container.

If you see page with error 500 and you find in stack trace message You're here, you have set the request mapping succesfully. It's straightforward, swiftly and with style.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜