我怎样才能 return 仅来自 Spring servlet 的文本?

How can I return text only from Spring servlet?

事情是这样的,我有一个单页应用程序,我想从一个 ajax 对象调用一个 servlet,并让从 servlet 返回的文本显示在我的网页上。

这是我如何做到的示例:

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src="jquery.js" type="text/javascript" ></script>
<script type="text/javascript">
    function submittext() {

        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", "submit", true);
        xmlhttp.send();
        document.getElementById("myTextField").innerHTML = xmlhttp.responseText;

    }

</script>
</head>
<body>


    <input type="text" name="mytext" />
    <br>
    <button onClick="submittext()">Submit</button>
    <br>        
    <div id="myTextField"></div>

</body>
</html>

web.xml

 <servlet>
        <servlet-name>testdogservlet</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>testdogservlet</servlet-name>
        <url-pattern>/submit</url-pattern>
    </servlet-mapping>

testdogservlet-servlet

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="world.hello.mytest" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

MyServlet.java

package world.hello.mytest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyServlet {


    private String savedText;
    @RequestMapping("/submit")
    public ModelAndView submitText()
    {
        return new ModelAndView("text", "barModelName", "submission successful ModelObject");

    }



}

text.jsp

${barModelName};

这个解决方案工作正常。您单击提交按钮,它会将 div 内容更改为 text.jsp 中包含的文本。

问题是,将整个页面专门用作保存此文本的容器似乎是多余的。这样做有更整洁的解决方案吗?

只需将 return 对象替换为如下内容:

private String savedText;
    @RequestMapping("/submit")
    public String submitText()
    {
        return "submission successful ModelObject";

    }

我的建议是使用@RestController 或 Jackson JSON 转换器将其转换为 JSON 对象。

spring 控制器可以将 HttpServletRequest 和 HttpServletReponse 传入映射的方法。

修改您的方法以接受这两个参数,然后您可以直接修改HttpServletResponse。

@RequestMapping("/submit")
private void submit(HttpServletRequest request, HttpServletResponse response) throws IOException
{           
    OutputStream os = response.getOutputStream();
    os.write("submission successful ModelObject".getBytes());
    os.close();
    os.flush();     
}
@RequestMapping("/submit")
@ResponseBody
public String submitText()
{
    return "submission successful ModelObject";
}