一、Struts2大致流程图
二、简单的样例
1、注册页面
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>reg.jsp</title>
</head>
<body>
<s:debug />
<s:form namespace="/regns" action="RegAction_reg" method="post" enctype="multipart/form-data">
<s:textfield name="name" label="UserName" />
<s:textfield name="age" label="UserAge" />
<s:file name="photo" label="UserPhoto" />
<s:submit />
</s:form>
</body>
</html>
2、注册成功页面
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.jsp</title>
</head>
<body>
you input's name is <s:property value="name" />.<br>
you input's age is <s:property value="age" />.<br>
</body>
</html>
3、注册失败页面
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>error.jsp</title>
</head>
<body>
error !!
</body>
</html>
4、struts配置
<?xml version="1.0"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 通过常量方式改变struts默认的属性配置, -->
<constant name="struts.action.extension" value="do" />
<!-- 设置开发模式,重新加载国际化资源文件和配置文件 -->
<constant name="struts.devMode" value="true" />
<!-- 动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="HelloWorldPkg" namespace="/helloworld" extends="struts-default">
<!-- 默认该包下默认的action引用,若该包没有要访问的action元素,则使用默认的action引用 -->
<default-action-ref name="HelloWorldAction" />
<!-- 设置默认的class引用,将摸个类作为默认的action类 -->
<default-class-ref class="cn.itcast.struts2.action.HelloWorldAction" />
<!-- 通过通配符的方式实现动态方法调用 -->
<action name="HelloWorldAction_*" class="cn.itcast.struts2.action.HelloWorldAction" method="{1}">
<result name="success">/index.jsp</result>
<result name="save">/index.jsp</result>
<result name="update">/success.jsp</result>
</action>
<!-- 没有类的action -->
<action name="ActionNoClass">
<result>/index.jsp</result>
</action>
</package>
<!-- 模块话编程,包含配置文件 -->
<include file="cn/itcast/struts2/action/reg.xml" />
</struts>
5、reg.xml配置
<?xml version="1.0"?>
<!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="RegPkg" namespace="/regns" extends="struts-default">
<action name="RegAction_*" class="cn.itcast.struts2.action.RegAction" method="{1}">
<result name="success">/reg/success.jsp</result>
<result name="error">/reg/error.jsp</result>
<result name="regView">/reg/reg.jsp</result>
<result name="input">/reg/reg.jsp</result>
<interceptor-ref name="defaultStack">
<param name="validation.excludeMethods">input,back,cancel,toRegView</param>
</interceptor-ref>
</action>
</package>
</struts>
5、RegAction
package cn.itcast.struts2.action;
import java.io.File;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.validation.SkipValidation;
import com.opensymphony.xwork2.ActionSupport;
public class RegAction extends ActionSupport {
private static final long serialVersionUID = 2941355104469235318L;
private String name;
private Integer age;
/* 接受上传的文件 */
private File photo ;
/* 接受文件名称 */
private String photoFileName ;
/* 接受文件内容类型的 */
private String photoContentType ;
public String getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public String getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
/**
*
*/
public String reg(){
System.out.println("reg : " + name);
String dir = ServletActionContext.getServletContext().getRealPath("/upload");
String ext = photoFileName.substring(photoFileName.lastIndexOf("."));
long l = System.nanoTime();
File newFile = new File(dir,l + ext);
photo.renameTo(newFile);
return SUCCESS;
}
/**
* 达到注册页面
*/
//@SkipValidation
public String toRegView(){
return "regView" ;
}
/**
*
*/
public void validate() {
if(name == null || name.length() == 0){
addFieldError("name",getText("error.name.empty"));
}
}
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
}
6、将校验错误信息放置配置文件中;
error.name.empty=name is required!
error.name.age=age is required!