I have the following controller
#Controller
#RequestMapping("/room/{roomId:^(?!main.html$).*}")
public class Rooms {
#RequestMapping
public String index(#PathVariable(value = "roomId") String id) {
// do some stuff...
return "main.html";
}
}
I have a main.html file in my resource/static folder. I would like when a user goes to /room/{something} to do some processing, return the static main.html page I have.
Everything I try results in a page not found exception or
javax.servlet.ServletException: Circular view path [main.html]:
would dispatch back to the current handler URL
[/room/main.html] again
what am I missing to allow this to work?
Thanks for the help
If you are doing configuration in class for view resolver
#Bean
public UrlBasedViewResolver urlBasedViewResolver()
{
UrlBasedViewResolver res = new InternalResourceViewResolver();
res.setViewClass(JstlView.class);
res.setPrefix("/WEB-INF/");
res.setSuffix(".jsp");
return res;
}
If using xml
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/jsp/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
Also change your controller by below code
#RestController
#RequestMapping("/room/")
public class Rooms {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView index(#PathVariable(value = "roomId") String id) {
// do some stuff...
ModelAndView model = new ModelAndView();
model.setViewName("main");
return model;
}
}
Related
I am using spring mvc(version:4.1.5.RELEASE). I have added new controller in my project and trying to use #PathVariable annotation. As I add this annotation I get 404 error of my jsp template view page. If I remove #PathVariable annotation and {id} from #RequestMapping path, code works properly. Can someone please help me whats wrong?
dispatcher-servlet.xml
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
my new controller
#Controller
public class MoviesController {
#RequestMapping(value = "/movie/{id}", method=RequestMethod.GET)
public String moviesDetails(#PathVariable(value="id")
String id, ModelMap map) {
System.out.println(id);
map.addAttribute("movieId", id);
return "moviesDetails";
}
}
link where i am adding id is like below.
a href="movie/${movie.id}">${movie.movieName}
HTTP Status 404 - /shcm/movie/WEB-INF/views/moviesDetails.jsp
moviesDetails.jsp file already exists in WEB-INF/views path
Change in controller :
#GetMapping("/movie/{id}")
public String moviesDetails(#PathVariable
String id, ModelMap map) {
System.out.println(id);
map.addAttribute("movieId", id);
return "moviesDetails";
}
And in web.xml :
<property name="prefix" value="/WEB-INF/views/"></property>
Change
#PathVariable(value="id") String id
to
#PathVariable("id") String id
I am developing a spring boot application, I am trying to use XmlViewResolver to view the pdf from predefined template using itext. Successfully getting the jsp page when I access the "/welcome" and "/invoice", but failed when I try "/pdf".
Please let me know what's wrong. Why the pdf bean is not working as expected?
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Not Found, status=404).
/WEB-INF/jsp/pdf.jsp
Below are the codes
#Controller
public class MainController {
#RequestMapping("/welcome")
public String hello(Model model) {
model.addAttribute("msg", "XmlViewResolver Demo");
return "success";
}
#RequestMapping("/invoice")
public String getInvoiceInfo() {
return "invoice";
}
#RequestMapping("/pdf")
public String getPdfInfo() {
return "pdf";
}
}
JspConfig.java
#Component
public class JspConfig {
#Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/jsp/");
bean.setSuffix(".jsp");
bean.setOrder(2);
return bean;
}
}
XmlConfig.java
#Configuration
public class XmlConfig {
#Bean
public XmlViewResolver xmlViewResolver() {
XmlViewResolver resolver = new XmlViewResolver();
Resource resource = new ClassPathResource("xml/user-view.xml");
resolver.setLocation(resource);
resolver.setOrder(1);
return resolver;
}
}
user-view.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="invoice" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="WEB-INF/template/invoice.jsp" />
</bean>
<bean id="pdf" class="com.example.config.PdfView">
<property name="url" value="WEB-INF/template/invoiceTemplate.pdf" />
</bean>
PdfView.java
import com.lowagie.text.pdf.PdfStamper;
public class PdfView extends AbstractPdfStamperView implements MessageSourceAware {
#Override
protected void mergePdfDocument(Map<String, Object> model, PdfStamper stamper, HttpServletRequest request, response) throws Exception {
stamper.setFormFlattening(true);
String customerName = (String) model.get("customerName");
stamper.getAcroFields().setField("name", customerName);
stamper.close();
}
}
I have a controller in spring mvc:
#Controller
#RequestMapping(value = "/advertiser")
public class AdvertiserController {
#RequestMapping(value="/add2", method = RequestMethod.GET)
public ModelAndView add2(ModelAndView mav){
mav.addObject("command", new Advertiser());
mav.setViewName("advertiser/add");
return mav;
}
}
My viewResolver:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/"
p:suffix=".jsp" />
You may see, i have mapped the request to /advertiser/add2 url and i want to serve it with the view named add.jsp (advertiser/add). However when run the web app, it always show up the add2.jsp (advertiser/add2) page. My jsp views are in /WEB-INF/advertiser directory.
What i did wrong here?
I think your url mapping is incorrect.
try this.Remove advertiser from setViewName.
#Controller
#RequestMapping(value = "/advertiser")
public class AdvertiserController {
#RequestMapping(value="/add2", method = RequestMethod.GET)
public ModelAndView add2(ModelAndView mav){
mav.addObject("command", new Advertiser());
mav.setViewName("add");
return mav;
}
}
I'm exploring spring mvc but have issue when bind model to view.
My controller:
#Controller
#RequestMapping(value = "/advertiser")
public class AdvertiserController {
#Autowired
private AdvertiserService advertiserService;
#RequestMapping(value="/edit/{id}", method = RequestMethod.GET)
public ModelAndView edit(#PathVariable long id){
Advertiser adv=advertiserService.get(id);
return new ModelAndView("advertiser/edit","command",adv);
}
}
i also have a edit.jsp file in /WEB-INF/advertiser directory.
My viewresolver:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/"
p:suffix=".jsp" />
However, i just got 404 error when go to: http://localhost:8080/springmvcdemo/advertiser/edit/1
The error description: /springmvcdemo/WEB-INF/advertiser/edit/1.jsp
It looks like spring is trying to find a 1.jsp file to serve the request instead of serve with edit.jsp as i want.
UPDATE:
I just add 1 more function to my controller and it not work also:
#RequestMapping(value="/add2", method = RequestMethod.GET)
public ModelAndView add2(ModelAndView mav){
mav.addObject("command", new Advertiser());
mav.setViewName("advertiser/add");
return mav;
}
And the issue is still, the 404 error with the description:
/springmvcdemo/WEB-INF/advertiser/add2.jsp
I have point the view to "advertiser/add" but spring still trying to find add2.jsp not add.jsp. That make me think i failed in the viewResolver configuration but still not find out what it is.
Change the path to advertiser/edit/1 as follows..
#RequestMapping(value="/edit/{id}", method = RequestMethod.GET)
public ModelAndView edit(#PathVariable long id){
Advertiser adv=advertiserService.get(id);
return new ModelAndView("advertiser/edit","command",adv);
}
you need to check point.
servelt-context.xml
<!-- Resolves views selected for rendering by #Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
controller
#Controller
#RequestMapping(value = "/advertiser")
public class AdvertiserController {
#Autowired
private AdvertiserService advertiserService;
#RequestMapping(value="/edit/{id}", method = RequestMethod.GET)
public ModelAndView edit(#PathVariable long id, ModelAndView mav){
Advertiser adv = advertiserService.getAdvertiserById(id);
mav.addObject("command", adv);
mav.setViewName("folder/viewName");
return mav;
}
}
Example directory : webapp/WEB-INF/views/advertiser/edit.jsp
Example case : mav.setViewName(advertiser/edit); using viewName.
I think to change your code. if your directory WEB-INF/advertiser/add2.jsp
YOUR UPDATE:
mav.setViewName("advertiser/**add2**");
SAMPLE CODE
Your have file(WEB-INF/advertiser/add and add2.jsp) in directory.
show add.jsp
#RequestMapping(value="/one", method = RequestMethod.GET)
public ModelAndView edit(ModelAndView mav){
mav.setViewName("advertiser/add");
return mav;
}
show add2.jsp
#RequestMapping(value="/two", method = RequestMethod.GET)
public ModelAndView edit(ModelAndView mav){
mav.setViewName("advertiser/add2");
return mav;
}
I am developing a simple spring application. I have a few jsps and I would like to change the name and the URL of a jsp. I changed the controller:
#RequestMapping(value = "/simpleForm.html", method = RequestMethod.GET)
public void simpleForm(Model model) {
model.addAttribute(new User());
}
to
#RequestMapping(value = "/newName.html", method = RequestMethod.GET)
public void simpleForm(Model model) {
model.addAttribute(new User());
}
and the name of the old simpleForm.jsp to newName.jsp user is a class I use in the form in simpleform.jsp
I couldn't make it work. I am getting 404 that simpleform.jsp is not found. I am pretty stuck.
Edit: My view resolver tags:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
My url pattern is like:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/forms/*</url-pattern>
</servlet-mapping>
I've found out that all of the links are getting the same error ()
resource not available. Even the ones that I didn't change the name
of.
I also tried directly starting from newName.jsp. Still the same error!
change:
#RequestMapping(value = "/newName.html", method = RequestMethod.GET)
to:
#RequestMapping(value = "/newName.jsp", method = RequestMethod.GET)
Double-check you still have the #Controller annotation on the class. I've seen 404s when has been accidentally deleted.
Try changing it to
#RequestMapping(value = "/newName.html", method = RequestMethod.GET)
public String simpleForm(Model model) {
model.addAttribute(new User());
return "newName"; // returning the desired view
}
And make sure you have defined viewResolver accordingly like
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
Change prefix as per your application structure.It will take the view (String returned by controller) and will add prefix and suffix. So the view resolved will be /WEB-INF/jsp/newName.jsp.
A 404 error is an HTTP status code that means that the page you were trying to reach on a website couldn't be found on their server.
As I can see, your suffix is .jsp. So try this (if there is any newName.jsp in the WebContent)
#RequestMapping(value = "/newName", method = RequestMethod.GET)
public void simpleForm(Model model) {
model.addAttribute(new User());
}
It should work
If you try this URL
http://localhost:<port>/AppNAme/forms/newName.html
and your controller is
#RequestMapping(value = "/newName.html", method = RequestMethod.GET)
public ModelAndView simpleForm(Model model) {
model.addAttribute(new User());
ModelAndView mv = new ModelAndView("jspViewName");
return mv;
}
The forward slash is missing in the prefix of your ViewResolver. This should fix your 404. Give it a shot.
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>