© 版權聲明:本文為博主原創文章,轉載請注明出處
Struts2處理結果類型
- SUCCESS:Action正確的執行完成,返回相應的視圖,success是name屬性的默認值
- ERROR:表示Action執行失敗,返回到錯誤處理視圖
- NONE:表示Action正確的執行完成,但是不返回任何視圖
- LOGIN:Action因為用戶沒有登錄的原因沒有正確執行,將返回登錄視圖,要求用戶進行登錄驗證
- INPUT:Action執行,需要從前端頁面獲取參數,input就是代表這個參數輸入的界面,一般應用中會對這些參數進行驗證,如果驗證沒有通過,將自動返回該視圖
實例:
1.項目結構

2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.struts</groupId>
<artifactId>Struts2-INPUT</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Struts2-INPUT Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<struts.version>2.5.10</struts.version>
</properties>
<dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
</dependencies>
<build>
<finalName>Struts2-INPUT</finalName>
</build>
</project>
3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<welcome-file-list>
<welcome-file>user.jsp</welcome-file>
</welcome-file-list>
<!-- struts2過濾器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4.UserAction.java
package org.struts.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private int age;
public String save() {
return SUCCESS;
}
@Override
public void validate() {
if (username == null || "".equals(username)) {
this.addFieldError("username", "用戶名不能為空");
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
5.struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/">
<action name="user" class="org.struts.action.UserAction" method="save">
<result>/success.jsp</result>
<result name="input">/user.jsp</result>
</action>
</package>
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
</struts>
6.messageResource.properties
# 方式一:統一指定類型轉換失敗的錯誤提示信息 {0}表示輸入框的name值
#xwork.default.invalid.fieldvalue={0} failure
# 方式二:單獨指定某個輸入框類型轉換失敗的提示信息,中文需轉換為對應的ASCII碼,否則頁面會亂碼
invalid.fieldvalue.age = \u5e74\u9f84\u8f93\u5165\u683c\u5f0f\u6709\u8bef
7.user.jsp
<%@ 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>用戶信息</title>
</head>
<body>
<form action="user" method="post">
姓名:<input type="text" name="username"/><s:fielderror fieldName="username"/><br/>
年齡:<input type="text" name="age"/><s:fielderror fieldName="age"/><br/>
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</form>
</body>
</html>
8.success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>成功頁面</title>
</head>
<body>
保存成功了...
</body>
</html>
9.效果預覽

10.格式轉換失敗
在action中的validate方法中可以對頁面屬性進行校驗,但是無法進行格式轉換失敗這樣的校驗(比如age的類型是int,但是前端頁面輸入abc),此時頁面會提示內置的錯誤提示

此時可以通過在struts.xml中指定<constant name="struts.custom.i18n.resources" value="messageResource">進行配置,修改提示信息
具體配置可以查看messageResource.properties中的注釋
