Struts2学习九----------处理结果类型(input)

© 版权声明:本文为博主原创文章,转载请注明出处

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 = u5e74u9f84u8f93u5165u683cu5f0fu6709u8bef

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中的注释

原文地址:https://www.cnblogs.com/jinjiyese153/p/6899847.html