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>
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'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'm debugging a controller method in Spring MVC that should return a view, but I'm getting a 404 page.
Error
HTTP Status 404
/WEB-INF/views/responsive/pages/company/myCompanyCreatePurchaseOrderPage.jsp
The view is actually located in
/WEB-INF/views/pages/company/myCompanyCreatePurchaseOrderPage.jsp
Code
#RequestMapping(method = RequestMethod.GET, value = "/admin/getPoNumbers", produces = "application/json")
protected String getPurchaseOrdersFromSettings(#RequestParam(value = "uid")
final String uid, final Model model, final HttpServletRequest request, final RedirectAttributes redirectAttributes) {
// unrelated code omitted
return "pages/company/myCompanyCreatePurchaseOrderPage";
}
Where does this prefix get set?
InternalResourceViewResolver bean on your web.xml or applicationContext.xml
Sample
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
I have spring mvc application. I have 2 controllers -
first uses standart view resolver and render to jsp
second uses resolver for creatin RestFull wev service
How I can configure it?
UPDATE
first controller
#Controller
#RequestMapping("/controllerPath")
public class MyController {
#RequestMapping("/sayHello")
public String sayHello(Model model){
model.addAttribute("name", "Vasya");
return "hello";
}
}
it uses
<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>
I need to add one more controller which will work as restful webservice and return json to client
You can do a RESTful handler by simply having the Jackson libraries in your classpath and returning the object you want to serialize to JSON, or by using the #ResponseBody annotation to indicate that your returned object should be used as the "body" of the HTTP response directly.
Using Jackson
#RequestMapping("/myRestful")
MyDomainObject myRestfulService() {
// .. do some stuff... //
return new MyDomainObject(); // <- Return some object
}
Using #ResponseBody:
#RequestMapping("/myRestful")
#ResponseBody String myRestfulService() {
// .. do some stuff... //
return new "This is some string that will be returned, as-is, to the client"; // <- Return some data
}
Hope that helps
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>