weixin_33694620 2016-12-02 13:16 采纳率: 0%
浏览 32

Spring MVC Ajax

I want to run Ajax and showResults. I have created simple 2 JSP pages,1 CONTROLLER and 1 DOMAIN.I am using netbeans.I am unable to add users and see all user list.

AddUser. jsp

<script type="text/javascript">
        src = "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" >
                function doAjaxPost() {
                    var name = $('#name').val();
                    var education = $('#education').val();
                    $.ajax({
                        type: "POST",
                        url: "url",
                        data: "name=" + name + "&education=" + education,
                        success: function (response) {
                            $('#info').jsp(response);
                            $('#name').val('');
                            $('#education').val('');
                        },
                        error: function (e) {
                            alert('Error: ' + e);
                        }
                    });
                }
    </script>
</head>
<body>
    <h1>Add Users using Ajax ........</h1>
    <c:url var="user" value="/ShowUsers"/>
    <form:form method="POST" modelAttribute="user" action="${user}">
        <table>
            <tr><td>Enter your name : </td><td> <input type="text" id="name"><br/></td></tr>
            <tr><td>Education : </td><td> <input type="text" id="education"><br/></td></tr>
            <tr><td colspan="2"><input type="button" value="Add Users" onclick="doAjaxPost()"><br/></td></tr>
            <tr><td colspan="2"><div id="info" style="color: green;"></div></td></tr>
        </table>
        <a href="/ShowUsers">Show All Users</a>
    </form:form>`

ShowUsers.jsp

   <table>
        <tr>
            <td>Name</td>
            <td>${user.name}</td>
        </tr>
        <tr>
            <td></td>
            <td>${user.education}</td>
        </tr>

    </table>  

web.xml

  <servlet>
    <servlet-name>UserListController</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>UserListController</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

UserListController-servlet.xml

<context:component-scan base-package="com.tutorialspoints" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>  

User.java

public class User {

private String name;
private String education;


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEducation() {
    return education;
}

public void setEducation(String education) {
    this.education = education;
}

UserListController.xml

@Controller
public class UserListController {

private final List<User> userList = new ArrayList<User>();

@RequestMapping(value = "/", method = RequestMethod.GET)
public String showForm(Model m) {
    m.addAttribute("user", new User());
    return "AddUser";
}

@RequestMapping(value = "/AddUser", method = RequestMethod.POST)
public @ResponseBody
String addUser(@ModelAttribute(value = "user") User user, BindingResult result) {
    String returnText;
    if (!result.hasErrors()) {
        userList.add(user);
        returnText = "User has been added to the list. Total number of users are " + userList.size();
    } else {
        returnText = "Sorry, an error has occur. User has not been added to list.";
    }
    return returnText;
}

@RequestMapping(value = "/ShowUsers", method = RequestMethod.POST)
public String showUsers(@ModelAttribute User user, ModelMap model) {
    model.addAttribute("name", user.getName());
    model.addAttribute("education", user.getEducation());
    return "ShowUsers";
}
}

ShowUsers.jsp

<body>
    <table>
        <tr>
            <td>Name</td>
            <td>${user.name}</td>
        </tr>
        <tr>
            <td></td>
            <td>${user.education}</td>
        </tr>

    </table>  
</body>
  • 写回答

1条回答 默认 最新

  • weixin_33743248 2016-12-02 21:41
    关注
    • Pass you data as a simple json data: {'name': name, 'education': education}.
    • Change your ajax request as below
    • You were specifying them in query param string format which is wrong "name=" + name + "&education=" + education
    • Correct your url that you are passing to the server. It should end with xyzabc/AddUser

      $.ajax({ type: "POST", url: YOUR_ACTUAL_SERVER_URL, data: {'name': name, 'education', education}, success: function (response) { $('#info').jsp(response); $('#name').val(''); $('#education').val(''); }, error: function (e) { alert('Error: ' + e); } });

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动