读点儿设计 2018-04-11
前言:本文对案例练习一拓展,进行数据封装,并实现表单校验。
数据封装
1 修改struts.xml配置,将action添加class属性
<package name="default" namespace="/" extends="struts-default"> <action name="HelloWorld" class="action.HelloWorldAction"> <result>HelloWorld.jsp</result> </action> </package>
2 创建HelloWorld.java文件
package action; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @Override public String execute() throws Exception { setMessage("hello,world! 我来自HelloWorldAction.java"); return SUCCESS; } }
3,修改HelloWorld.jsp,引入struts2标签库。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>HelloWord</title> </head> <body> <h1><s:property value="message"/></h1> </body> </html>
运行结果