spring mvc cannot mapping request to other view name - java

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;
}
}

Related

How to access Model Attribute's value and set to ModelAndView

I cant seem to find the solution for this.
I have method that returns ModelAndView to view web pages.
I store a webpage filename to model whenever a condition is true.
Example:
#RequestMapping(value = "/process", method = RequestMethod.POST)
public ModelAndView processRequest(HttpServletRequest request,
HttpServletRequest response, Model model,#RequestParam("file") MultipartFile file)
throws IOException {
if (file.isEmpty()) {
model.addAttribute("exception", "web_file1")
} else {
model.addAttribute("exception", "web_file2")
}
How can I retrieve the data stored in "exception" and set it to ModelAndView?
ModelAndView mav = new ModelAndView();
mav.setViewName("exception");
//expected:web_file2
//actual:exception
return mav;
model.addAttribute("exception", "web_file2")
String sModel=model.toString(); //{exception=web_file2}
String returnView = (sModel).substring(11,sModel.length()-1); //web_file2
return new ModelAndView(returnView);
I've found a way to get it,
But I think there's a better way to do this.
1. put your data using
mv.addObject("exception","web_file1");
in your view retrieve data by the key ie exception ${exception}
eg : <input type="text" value="${exception}" />
2. if used spring mvc with jsp/html Ensure you have declared a bean for viewResolver
which has following format
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>abc.jsp</value>
</property>
</bean>

I am using #PathVariable from springframework. When I try to return view page name from controller, I get 404 error

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

spring mvc not bind model to view

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;
}

Returning static page with url path parameters

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;
}
}

Change Name of a .JSP file

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>

Categories

Resources