我使用spring制作休息网络服务。 在rest client-ui的帮助下,我轻松插入更新并删除用户。 现在我想用User 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();
}
}
<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>
<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动作调用Controller addUser 我会非常感激
答案 0 :(得分:0)
创建POJO UserForm.java为
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页面代码将是
<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>
在Controller中你应该有2个方法
@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
}
}