我想使用 Spring 从 REST 服务生成原始 xml。在解决这些观点时有些东西会丢失。我得到以下信息:
javax.servlet.ServletException: Circular view path [RemedyXml]: would dispatch back to the current handler URL [/audiClave/REST/remedies/RemedyXml] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
这是 web.xml(缩写):
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/restServlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/REST/</url-pattern>
</servlet-mapping>
restServlet-context.xml 如下(略):
<context:component-scan base-package="com.audiClave.controllers" />
<bean id="beanNameResolver"
class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean id = "RemedyXml" class = "com.audiClave.views.RemedyXmlView"/>
现在对于控制器:
@Controller
public class RestController{
private static final String REMEDIES = "remedies";
@RequestMapping(value = "/REST/remedies/{language}", method = RequestMethod.GET)
protected ModelAndView getAllRemedies(HttpServletResponse response,
@PathVariable("language") String language) throws IOException {
Map<String, List<Remedy>> myModel = new HashMap<String, List<Remedy>>();
RemediesBean remediesBean = Factory.getRemediesBean();
myModel.put("remedies", remediesBean.loadRemedies(language));
return new ModelAndView("RemedyXml","model", myModel);
最后是视图:
public class RemedyXmlView implements View
{
/** XML document */
private Document doc = null;
public RemedyXmlView()
{
}
public String getContentType()
{
return "text/xml";
}
@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
throws Exception {
/** Generate the XML */
// Start document with "root" tag
this.doc = new Document(new Element("model"));
// Get the root tag
Element rootEl = this.doc.getRootElement();
// Add other tags
Map<String,List<Remedy>> myModel = (Map<String,List<Remedy>>)model.get("model");
List<Remedy> messages = (List<Remedy>)myModel.get("remedies");
Iterator<Remedy> iter = messages.iterator();
while(iter.hasNext()){
Remedy remedy = iter.next();
// Add new xml element
Element messageEl = new Element("remedy");
messageEl.setText(remedy.getName());
rootEl.addContent(messageEl);
}
// printXMLDocument(this.doc);
/** Set response type and write XML */
XMLOutputter outp = new XMLOutputter();
outp.setFormat(Format.getPrettyFormat());
String xmlAsString = outp.outputString(doc);
response.setContentType("text/xml");
response.setContentLength(xmlAsString.length());
PrintWriter out = new PrintWriter(response.getOutputStream());
out.print(xmlAsString);
out.flush();
out.close();
}
}
任何输入将不胜感激,谢谢。