setting fullpath in controller
i have developed a spring application. all requests are dispatching to controllers (i have 2 controllers in my app) so web.xml is like below
in web.xml
<servlet-mapping>
<url-pattern>/*</url-pattern>
aaa controller开发者_Go百科
@Controller
@RequestMapping("/aaa")
bbb controller
@Controller
@RequestMapping("/bbb")
but now i need to add some jsp pages into my project since the "/*" in web.xml my jsp pages are not found. so i have change the servlet-mapping like below;
in web.xml
<servlet-mapping>
<url-pattern>/aaa/*</url-pattern>
<url-pattern>/bbb/*</url-pattern>
aaa controller
@Controller
@RequestMapping("/")
bbb controller
@Controller
@RequestMapping("/")
but i do not want to use this approach since i can access xxx servlet in aaa controler like /bbb/xxx.
so is there any alternative solution, for example can i set full path in controller or anything?
thanks in advance...
You need to pass jsp through the server as well. You can map it as html extension
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
In example-servlet.xml just add the following jsp resolver
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
and then use ModelAndView Object in your controllers:
@Controller
@RequestMapping(value="/aaa")
public class aaaController{
@RequestMapping(value="/aaa.html", method=RequestMethod.GET)
public ModelAndView index(){
ModelAndView mv = new ModelAndView("aaa");
return mv;
}
}
@Controller
@RequestMapping(value="/bbb")
public class aaaController{
@RequestMapping(value="/bbb.html", method=RequestMethod.GET)
public ModelAndView index(){
ModelAndView mv = new ModelAndView("bbb");
return mv;
}
}
In that case first controller will return /aaa.jsp as you your model andView when you hit /aaa/aaa.html
and second controller will return /bbb.jsp as you your model and View when you hit /bbb/bbb.html
Hope it helps.
精彩评论