struts2标签下的用户注册

来源:互联网 发布:ireport和java 编辑:程序博客网 时间:2024/05/29 08:19

实现效果:




1、register.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix = "s" uri = "/struts-tags"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'register.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    <s:form action = "Register" method = "post">      <s:textfield name = "username" label = "用户名"></s:textfield><br>      <s:password name = "password" label = "密码"></s:password><br>      <s:radio list="#{0:'男',1:'女' }" name = "sex" label = "性别"></s:radio><br>      <s:select list="{'请选择省份','吉林','四川','重庆','湖北','广西'}" label = "省份" name = "province"></s:select><br>      <s:checkboxlist list="{'足球','篮球','羽毛球'}" name = "hobby" label = "爱好"></s:checkboxlist><br>      <s:submit value = "注册"></s:submit>    </s:form>  </body></html>

2、RegisterAction:

package com.action;import com.bean.User;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class RegisterAction extends ActionSupport implements ModelDriven<User>{  private User user = new User();    public User getModel() {return user;   }    public String execute() {return SUCCESS;}}

3、register_OK.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix = "s" uri = "/struts-tags"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'register_OK.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    姓名:<s:property value = "username"/><br><br> 性别:<s:property value = "sex"/><br><br> 省份:<s:property value = "province"/><br><br> 爱好:<s:property value = "hobby"/><br><br>  </body></html>


4、javaBean

User.java:

package com.bean;public class User {  private String username ;  private String password ;  private String sex;  private String province;  private String hobby;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 getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getProvince() {return province;}public void setProvince(String province) {this.province = province;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby = hobby;}      }

5、struts.xml文件:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"    "http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts>  <package name="default" namespace = "/" extends = "struts-default">    <action name="Register" class = "com.action.RegisterAction">       <result name = "success">/register_OK.jsp</result>    </action>  </package></struts>