Struts2 - Form Tags_HelloWorld升级版(从零开始学习Strust2_02)

来源:互联网 发布:raysource mac 编辑:程序博客网 时间:2024/05/16 12:18

开发环境:

Eclipse IDE for Java EE Developers(下载地址)

struts-2.3.1.2(下载地址)

apache-tomcat-6.0.35(下载地址)


结果图:





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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>struts2_20120311_02</display-name><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

index.jsp修改为一个注册的界面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@taglib uri="/struts-tags" prefix="s"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Struts Basic UI</title></head><body><s:form action="register"><s:textfield name="username" label="User Name"></s:textfield><s:password name="password" label="PassWord"></s:password><s:radio name="gender" label="Gender" list="{'Male','Female'}"></s:radio><s:select name='location' label="Location"list="{'Beijing','Shanghai','Shenzhen','Guangzhou','Hangzhou','Chengdu','Wuhan'}"></s:select><s:checkboxlist name="community" label="community"list="{'C','C++','Java','C#','Ruby On Rails','Objective-C','Android'}"></s:checkboxlist><s:textarea name="about" label="about you"></s:textarea><s:submit></s:submit></s:form></body></html>

success.jsp显示的内容更多了,原理和Helloworld一样

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib uri="/struts-tags" prefix="s"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>success</title></head><body>User Name:<s:property value="username" /><br> Gender:<s:property value="gender" /><br> Location:<s:property value="location" /><br> Community:<s:property value="community" /><br>About:<s:property value="about"/></body></html>

RegisterAction.java

package com.zeph.struts2;import com.opensymphony.xwork2.ActionSupport;public class RegisterAction extends ActionSupport {private String username;private String password;private String gender;private String location;private String community;private String about;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 getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getLocation() {return location;}public void setLocation(String location) {this.location = location;}public String getCommunity() {return community;}public void setCommunity(String community) {this.community = community;}public String getAbout() {return about;}public void setAbout(String about) {this.about = about;}public String execute() {return SUCCESS;}}

struts.xml没有修改,只是改了下action的name和class的内容

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="default" extends="struts-default"><action name="register" class="com.zeph.struts2.RegisterAction"><result name="success">/success.jsp</result></action></package></struts>


PS:Form Tag中含有List属性的标签也可以使用在Struts2_04用到的Properties的方法。这样可以更加方便的使用和修改资源。

原创粉丝点击