Struts2标签下的用户注册

Struts2标签下的用户注册Struts2标签下的用户注册通过Struts2框架提供的表单标签编写用户注册表单,将用户的注册信息输出到JSP页面中。1、创建动态JavaWeb项目,将Struts2的相关类包添加到项目的classpath,并在web.xml文件中注册提供的StrutsPrepareAndExecuteFilter过滤器。相关类包如下图所示:2、创建index.jsp页面,在主页面中通过

Struts2标签下的用户注册

通过Struts2框架提供的表单标签编写用户注册表单,将用户的注册信息输出到JSP页面中。

1、创建动态JavaWeb项目,将Struts2的相关类包添加到项目的classpath,并在web.xml文件中注册提供的StrutsPrepareAndExecuteFilter过滤器。相关类包如下图所示:

Struts2标签下的用户注册

2、创建index.jsp页面,在主页面中通过Struts2框架提供的表单标签编写用户注册的表单。关键代码如下:

<body>
<h2>用户注册</h2>
<s:form action="userAction" method="post">
<s:textfield name="name" label="用户名" requried="true" requiredposition="left"></s:textfield>
<s:password name="password" label="密码" required="true" requiredposition="left"></s:password>
<!-- list的取值是一个ONGL表达式,创建了一个Map对象。必须有name -->
<s:radio name="sex" list="#{1:'男',0:'女' }" label="性别" required="true" requiredposition="left"></s:radio>
<!-- list的取值是一个ONGL表达式,创建了一个List对象。必须有name -->
<s:select list="{'请选择省份','吉林','广东','山东','河南'}" name="province" label="省份"></s:select>
<s:checkboxlist list="{'足球','篮球','羽毛球','乒乓球'}" name="hobby" label="爱好"></s:checkboxlist>
<s:textarea name="description" cols="30" rows="5" label="描述"></s:textarea>
<s:submit value="注册"></s:submit>
<s:reset value="重置"></s:reset>
</s:form>
</body>

3、创建用户注册成功页面success.jsp,在该页面中通过Struts2的数据标签将用户注册信息输出到页面中,关键代码如下:

<div>
<h2>用户注册信息</h2>
<ul>
<li>用户名:<s:property value="name"/></li>
<li>密码:<s:property value="password"/></li>
<li>性别:
<s:if test="sex==0">女</s:if>
<s:else>男</s:else>
</li>
<li>省份:<s:property value="province"/></li>
<li>爱好:<s:property value="hobby"/></li>
<li>描述:<s:property value="description"/></li>
</ul>
</div>

4、创建名称为UserAction的类,继承于ActionSupport类,是一个Action对象,它的作用是对用户注册请求以及用户信息编辑请求进行处理。代码如下:

package com.test.action;
import com.opensymphony.xwork2.ActionSupport;


public class UserAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String name;
private String password;
private String description;
private int sex = 0;
private String province;
private String hobby[];
public String execute() throws Exception{
return SUCCESS;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getSex() {
return sex;
}
public void setSex(int 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;
}
}

在UserAction类中,用户爱好属性是一个字符串数组,所以应将其定义为字符串数组变量hobby。

5、struts.xml配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="struts2" extends="struts-default">
<action name="userAction" class="com.test.action.UserAction">
<result>success.jsp</result>
</action>
</package>
</struts>

6、web.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>strutsWebTest</display-name>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

7、运行结果:

Struts2标签下的用户注册

提交结果页面:

Struts2标签下的用户注册

今天的文章Struts2标签下的用户注册分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/26281.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注