spring mvc 3 学习从页面到后台

我的例子:注册到保存数据

 

1 前台页面,采用spring 标签[注意标红色部分]

 

register.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

 

<form:form commandName="user" id="regform"  name="regform">
 <dl id=d_email>
 <dt><label for=regEmail>电子邮箱:</label></dt>
 <dd>
 <input id=username class=inputtext type=text name=username />
 <p><a href="#">电子邮箱作为账号的好处</a></p>
 </dd>
</dl>
<dl>
 <dt><label for=pwd>设置密码:</label></dt>
 <dd><input id=pwd class=inputtext maxLength=20 type=password
  name=password />
 </dd>
</dl>
<dl class=dl_pad>
 <dt><label for=nick>喜欢昵称:</label></dt>
 <dd><input id="nick" name="nick" class=inputtext/>
 </dd>
</dl>
<dl>
 <dt></dt>
 <dd><img id=verifypic src="resources/images/getcode.jpg" /><a
   href="javascript:refreshCode();">看不清换一张?</a></dd>
</dl>
<dl>
 <dt><label for=code>验证码:</label></dt>
 <dd><input id=icode class="inputtext validate-code" maxLength=5
  size=5 type=text name=icode /><span id=icode_error_span class=box-error><b
  id=icode_error_span_i></b></span></dd>
</dl>
<dl>
 <dt>&nbsp;</dt>
 <dd><input class="submit" type="submit" value="立即注册"/></dd>
</dl>
 </form:form>

 

后台:

package com.lsoba.ucenter.controller;

import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lsoba.model.UserAccount;
import com.lsoba.ucenter.service.UserService;
import com.lsoba.ucenter.service.UserValidator;

@Controller
@RequestMapping("/reg.htm")
public class RegController {

 private static final Logger log = LoggerFactory
   .getLogger(RegController.class);

 @Inject
 @Named("userService")
 UserService userService;

 @Inject
 @Named("userValidator")
 UserValidator userValidator;

 /**
  * 显示注册页面
  *
  * @return
  */
 @RequestMapping(params = "m=show")
 public String showReg(@ModelAttribute("user") UserAccount user,ModelMap model) {

/*如果把红色部分去掉就会报,Neither Errors instance nor plain target object for bean name 'command' 

 available as request attribute  model.addAttribute(user);

错误,表示非控制访问控制表单,意思就是页面的 <form:form commandName="user" id="regform"   name="regform">

才会起到作用。

*/
  log.info("into reg page");
  return "/usercenter/register";
 }

 /**
  * 保存用户信息
  *
  * @return
  */
 @RequestMapping(params = "m=save", method = RequestMethod.POST)
 public String saveUserInfo(@ModelAttribute("user") UserAccount user,ModelMap model) {
  model.addAttribute(user);
  return "";
 }

}

 

 

访问时:http://localhost:8080/reg.htm?m=show

可以进入注册页面。

你可能感兴趣的:(JavaScript,spring,mvc,Web,bean)