最近在学struts2,struts2相比以前的JSP+Servlet,在处理流程上的更简单,我们就一个小实例来具体分析一下。
实例内容如下:
实现一个简单的注册页面包括:用户名、密码、重复密码、年龄、出生日期、毕业日期
要求如下:
用户名的长度在4-6之间
密码的长度在4-6之间
重复密码与密码相等
年龄在10-50之间
出生日期在毕业日期之前
输入错误返回原页面,并在原页面的文本框后面显示具体的错误信息。正确输入则跳入下个页面将信息显示出来。
1、JSP+Servlet
1)我们编写注册页面register.jsp
注册页面,不使用struts标签库的话,基本上大同小异,唯一的区别在form中action选项上,这里面的servlet自然要跳到RegisterServlet上进行相应的信息处理。我们先各自处理,然后再来比较吧。
- <body>
- <h1>用户注册</h1>
- <form action="RegisterServlet" method="post">
- username:<input type="text" name="username"><br>
- password:<input type="password" name="password"><br>
- repassword:<input type="password" name="repassword"><br>
- age:<input type="text" name="age"><br>
- birthday:<input type="text" name="birthday"><br>
- graduate:<input type="text" name="graduate"><br>
- <input type="submit" value="submit">
- </form>
- </body>
2)因为这些属性都是属于注册用户的,我们再构建一个User类
- private String username;
- private String password;
- private String repassword;
- private int age;
- private Date birthday;
- private Date graduate;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getRepassword() {
- return repassword;
- }
- public void setRepassword(String repassword) {
- this.repassword = repassword;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public Date getGraduate() {
- return graduate;
- }
- public void setGraduate(Date graduate) {
- this.graduate = graduate;
- }
3)输入效验可以分为客户端效验和服务器端的效验,客户端的效验是将验证代码用javascript写到页面中,这里面我们统一用服务器端效验。编写RegisterServlet
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- //从页面传过来的数据都是字符形式的,我们要进行相应的转换
- String username=request.getParameter("username");
- String password=request.getParameter("password");
- String repassword=request.getParameter("repassword");
- int age=Integer.valueOf(request.getParameter("age"));
- String birthday=request.getParameter("birthday");
- String graduate=request.getParameter("graduate");
- Date graduate1=null;
- Date birthday1=null;
- Map<String, String> errors = new HashMap<String, String>();
- if (username == null)
- {
- errors.put("username", "用户名不能为空");
- } else
- {
- if (username.length()<4||username.length()>6)
- {
- errors.put("username", "用户名必须是4到6位");
- }
- }
- if (password == null)
- {
- errors.put("password", "密码不能为空");
- } else
- {
- if (password.length()<4||password.length()>6)
- {
- errors.put("password", "密码必须是4-6位数字");
- }
- }
- if (repassword ==null)
- {
- errors.put("repassword", "确认密码不能为空");
- } else
- {
- if (!(password.equals(repassword)))
- {
- errors.put("repassword", "确认密码必须与密码输入一致");
- }
- }
- if(age>50||age<10)
- {
- errors.put("age", "年龄无效");
- }
- if(birthday!=null)
- {
- DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
- try{
- birthday1=df.parse(birthday);
- }catch(Exception e)
- {
- errors.put("birthday", "日期格式不正确");
- }
- }else{
- errors.put("birthday", "生日不能为空");
- }
- if(graduate!=null)
- {
- DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
- try{
- graduate1=df.parse(graduate);
- }catch(Exception e)
- {
- errors.put("graduate", "日期格式不正确");
- }
- }else{
- errors.put("graduate", "毕业日期不能为空");
- }
- if(null!=graduate1&&null!=birthday1)
- {
- Calendar c1=Calendar.getInstance();
- c1.setTime(birthday1);
- Calendar c2=Calendar.getInstance();
- c2.setTime(graduate1);
- if(!c1.before(c2))
- {
- errors.put("graduate", "毕业日期出生日期不匹配");
- }
- }
- <span style="color:#FF0000;">if(errors.size()==0)
- {
- User user=new User();
- user.setUsername(username);
- user.setAge(age);
- user.setPassword(password);
- user.setRepassword(repassword);
- user.setBirthday(birthday1);
- user.setGraduate(graduate1);
- request.setAttribute("user", user);
- request.getRequestDispatcher("result.jsp").forward(request, response);
- }else
- {
- request.setAttribute("errors", errors);
- request.setAttribute("username", username);
- request.setAttribute("password",password);
- request.setAttribute("birthday", birthday);
- request.setAttribute("graduate",graduate);
- request.setAttribute("age",age);
- request.getRequestDispatcher("register.jsp").forward(request, response);
- }</span>
- }
4)然后我们编写一下result.jsp结果页面和修改一下register.jsp页面(出现错误后跳转,显示相应的错误信息)
result.jsp页面
- <body>
- username:${user.username}<br>
- password:${user.password}<br>
- age:${user.age}<br>
- birthday:${user.birthday}<br>
- graduate:${user.graduate}<br>
- </body>
register.jsp修改后内容
- <body>
- <h1>用户注册</h1>
- <form action="RegisterServlet" method="post">
- username:<input type="text" name="username" value="${username}">${errors.username}<br>
- password:<input type="password" name="password">${errors.password}<br>
- repassword:<input type="password" name="repassword">${errors.repassword}<br>
- age:<input type="text" name="age" value="${age}">${errors.age}<br>
- birthday:<input type="text" name="birthday" value="${birthday}">${errors.birthday}<br>
- graduate:<input type="text" name="graduate" value="${graduate}">${errors.graduate}<br>
- <input type="submit" value="submit">
- </form>
5)最后我们查看一下web.xml文件中,zai生成RegisterServlet时是否有相关的配置
- <servlet>
- <description></description>
- <display-name>ReigsterServlet</display-name>
- <servlet-name>ReigsterServlet</servlet-name>
- <servlet-class>ReigsterServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>ReigsterServlet</servlet-name>
- <url-pattern>/RegisterServlet</url-pattern>
- </servlet-mapping>
Ok,到这上面描述的Jsp+Servlet的基本功能都已实现,RegisterServlet作为整个代码的核心,写的是很长的。其实我们可以优化一下,将判断的内容放在formbean类中,然后在servlet中判调用。但是代码量是不减的,只能是让逻辑业务更清晰。
2、用Struts框架来实现以上功能。
1)首先struts的基本配置省略,可以参考http://blog.csdn.net/fumier/article/details/44626461
2)编写register.jsp页面,这里面使用了struts标签库,需要在开头第二行加入( <%@ taglib prefix="s" uri="/struts-tags"%>)
- <s:form action="register.action" theme="simple"><br>
- username:<s:textfield name="username" label="username"></s:textfield><br>
- password:<s:password name="password" label="password"></s:password><br>
- repassword:<s:password name="repassword" lable="repassword"></s:password><br>
- age:<s:textfield name="age" label="age"></s:textfield><br>
- birthday:<s:textfield name="birthday" label="birthday"></s:textfield><br>
- graduate:<s:textfield name="graduate" label="age"></s:textfield><br>
- <s:submit value="submit"></s:submit>
- </s:form>
3)接下来我们编写RegisterAction类
- public class RegisterAction extends ActionSupport {
- private String username;
- private String password;
- private String repassword;
- private int age;
- private Date birthday;
- private Date graduate;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getRepassword() {
- return repassword;
- }
- public void setRepassword(String repassword) {
- this.repassword = repassword;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public Date getGraduate() {
- return graduate;
- }
- public void setGraduate(Date graduate) {
- this.graduate = graduate;
- }
- @Override
- public String execute() throws Exception {
- // TODO Auto-generated method stub
- System.out.println("execute()");
- return SUCCESS;
- }
- @Override
- public void validate() {
- // TODO Auto-generated method stub
- System.out.println("validate()");
- if(null==username||username.length()<4||username.length()>6)
- {
- this.addFieldError("username","用户名无效");
- }
- if(null==password||password.length()<4||password.length()>6)
- {
- this.addFieldError("password","密码无效");
- }else if(null==repassword||repassword.length()!=password.length())
- {
- this.addFieldError("repassword","密码长度不一致");
- }else if(!repassword.equals(password))
- {
- this.addFieldError("repassword","密码不一致");
- }
- if(age<10||age>50)
- {
- this.addFieldError("age","年龄无效");
- }
- if(null==birthday)
- {
- this.addFieldError("birthday","出生日期为空");
- }
- if(null==graduate)
- {
- this.addFieldError("graduate","毕业日期为空");
- }
- if(null!=graduate&&null!=birthday)
- {
- Calendar c1=Calendar.getInstance();
- c1.setTime(birthday);
- Calendar c2=Calendar.getInstance();
- c2.setTime(graduate);
- if(!c1.before(c2))
- {
- this.addFieldError("graduate","毕业日期出生日期不匹配");
- }
- }
- }
- }
4)配置struts.xml文件
- <action name="register" class="cn.sict.register.RegisterAction">
- <result name="success">/result.jsp</result>//正确就跳转到result.jsp
- <result name="input">/register.jsp</result>//错误就跳转到register.jsp
- </action>
5)result.jsp与1中的相同,至此,就完成了所有的功能。
接下来,我们来看一下,使用struts框架,我们省略了哪些工作。
1)首先没有set/get方法了。
2)没有了servlet中从页面获得字符,然后再转换为需要的类型,然后简单的配置相应的result参数,就可以跳转到相应的页面。
3)继承了ActionSupport类重写validate()方法,使逻辑清晰。
Struts对上述的操作进行了包装,提高了程序的课重用性,我们只需要修改少量的参数和配置文件,就可以达到想要的效果。