Reputation: 245
I have a Java web service designed using Spring and a jsp file corresponding to it.
To further clarify, I have a jsp file called infosys.jsp WEB-INF/views which can be accessed like https:// localhost:8080/admin/infosys?id=34. I can pass in parameters too as shown.
In my web controller I have a method called
@RequestMapping(value = "/infosys", method = RequestMethod.GET)
public @ResponseBody void getId(@RequestParam(value="id", required=false) String id) {
.......
}
I have debugged and this method works, I can access the id passed it. I am trying to display the id in my jsp file. For example if I say https:// localhost:8080/admin/infosys I want to be able to display 34. How can I write to my jsp file? I have a MySQL db intergrate if i need to store and access. If I need that how can I write to DB and access??
EDIT: infosys.jsp file:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page isELIgnored ="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Info Sys</title>
</head>
<body>
<spring:url value="/" var="homeUrl"/>
<a href="${homeUrl}">Home</a>
<BR><BR>
</body>
</html>
Upvotes: 2
Views: 144
Reputation: 48287
Your Controller's Handler Method should return a String (no @ResponseBody) with the name of the view. You should also set your ID in a model.
@RequestMapping(value = "/infosys", method = RequestMethod.GET)
String getId(@RequestParam(value="id", required=false) String id, org.springframework.ui.Model model) {
model.addAttribute("id", id);
return "admin/infosys";
}
You should use Spring's InternalResourceViewResolver to turn that returned value into the relative path to the view. You would add that as a bean in your Web Application Context Configuration file, usually at /WEB-INF/mvc-dispatcher-servlet.xml
.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
admin/infosys
-> InternalResourceViewResolver -> /WEB-INF/views/admin/infosys.jsp
;
Here's how it works:
In your JSP file, simple put ${id}
anywhere in the page and this will be show the value of the "id" model attribute.
Upvotes: 6