0

我正在使用弹簧制作休息网络服务。在 rest client-ui 的帮助下,我可以轻松插入更新和删除用户。现在我想对用户 JSP 页面做同样的事情,但现在无法调用用户控制器

用户.jsp

<form action="adduser" name="user" id="my-form",method="POST"
    onsubmit="com.mobile-app.controller"/>

用户控制器

@RequestMapping(method = RequestMethod.POST, value = "/adduser", headers="Accept=application/xml, application/json")

    public void createuser(@RequestBody User user) {
        try {
            userManager.AddToUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Web.xml

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>rest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>Index.jsp</welcome-file>
    </welcome-file-list>

休息-Servlet.XML

<context:component-scan base-package="controller" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix">
            <value>/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <bean id="adduser" class="com.mobileapp.controller.UserController">
        <property name="userManager" ref="userManager" />
    </bean>

请帮助我通过 JSP 操作调用控制器 addUser 我将非常感谢

4

1 回答 1

0

Create a POJO UserForm.java as

public class UserForm {

    private Long id;
    @NotEmpty(message = "Please enter name")
    private String name;
    @NotEmpty(message = "Please enter username")
    private String username;
    @NotEmpty(message = "Please enter password")
    private String password;
    private String address;
    private String gender;
    @Future(message = "Please enter valid DOB")
    private Date dob;
    private String email;
    private String mobile;
//Getter & Setter
}

user.jsp page code will be

 <c:url var="saveOrUpdateUrl" value="/app/user/saveOrUpdate" />
 <form:form action="${saveOrUpdateUrl}" method="post" modelAttribute="userForm" id="userForm">
<form:input path="name"/>
// Other input fields
<input name="saveBtn" value="Save" type="submit">
</form:form>

In Controller you should have 2 method as

@Controller
@RequestMapping("/user")
public class UserController {

 @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        UserForm userForm = new UserForm();
        model.addAttribute("userForm", userForm);
        return "user";
    }

    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    public String saveOrUpdate(@RequestParam("id") Long id, @Valid UserForm userForm, BindingResult result, Model model) {

            //Logic to save data from userForm
    }


}
于 2012-07-03T06:10:04.993 回答