第八篇——Struts2的处理结果类型
生活随笔
收集整理的這篇文章主要介紹了
第八篇——Struts2的处理结果类型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Struts2處理結果類型
? ? 1、SUCCESS:表示Action正確的執行完成,返回相應的視圖,success是name屬性的默認值;
? ? 2、ERROR:表示Action執行失敗,返回到錯誤處理視圖;
? ? 3、NONE:表示Action正確的執行完成,但是不返回任何視圖;
? ? 4、LOGIN:Action因為用戶沒有登錄的原因沒有正確執行,將返回登錄視圖,要求用戶進行登錄驗證;
? ? 5、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>com.ray</groupId><artifactId>struts2Test</artifactId><packaging>war</packaging><version>1.0-SNAPSHOT</version><name>struts2Test Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core --><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.5.16</version></dependency></dependencies><build><finalName>struts2Test</finalName></build></project>3、web.xml
<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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0" metadata-complete="true"><display-name>Archetype Created Web Application</display-name><!-- 過濾所有請求交給Struts2處理 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class><!--action后綴(方法二)--><!--<init-param>--><!--<param-name>struts.action.extension</param-name>--><!--<param-value>ray</param-value>--><!--</init-param>--></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>4、UserAction.java
package com.ray.action;import com.opensymphony.xwork2.ActionSupport;/*** Created by Ray on 2018/3/26 0026.**/ public class UserAction extends ActionSupport {private String username;private int age;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;}public String save(){return SUCCESS;}@Overridepublic void validate() {if(username == null || "".equals(username)){this.addFieldError("username","用戶名不能為空");}} }5、eighth-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="com.ray.action.UserAction" method="save"><result>/loginSuccess.jsp</result><result name="input">/user.jsp</result></action></package><constant name="struts.custom.i18n.resources" value="messageResource"/> </struts>6、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><!-- action后綴(方法一) --><!--<constant name="struts.action.extension" value="ra"/>--><package name="default" namespace="/" extends="struts-default"><!-- 默認action --><default-action-ref name="404"/><action name="404"><result>/404.jsp</result></action><action name="helloWorld" class="com.ray.action.HelloWorldAction"><result name="success">/success.jsp</result></action></package><include file="second-struts.xml"/><include file="third-struts.xml"/><include file="fourth-struts.xml"/><include file="seventh-struts.xml"/><include file="eighth-struts.xml"/> </struts>7、messageResource.properties
# 方式一:統一指定類型轉換失敗的錯誤提示信息 {0}表示輸入框的name值 #xwork.default.invalid.fieldvalue={0} failure # 方式二:單獨指定某個輸入框類型轉換失敗的提示信息,中文需轉換為對應的ASCII碼,否則頁面會亂碼 invalid.fieldvalue.age = \u5e74\u9f84\u8f93\u5165\u683c\u5f0f\u6709\u8bef8、user.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %> <%String path = request.getContextPath();String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head><base href="<%=basePath%>"><title>用戶信息</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><s:form action="user" method="POST"><s:textfield label="姓名:" name="username"/><s:fielderror fieldName="username"/><br><s:textfield label="年齡:" name="age"/><s:fielderror fieldName="age"/><br><s:submit label="提交"/></s:form> </body> </html>9、loginSuccess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %> <%String path = request.getContextPath();String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head><base href="<%=basePath%>"><title成功頁面</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>保存成功! </body> </html>10、頁面效果
ok!
轉載于:https://www.cnblogs.com/Remenber-Ray/p/8858041.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的第八篇——Struts2的处理结果类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP-FPM Fastcgi 未授权访
- 下一篇: Apache Shiro<=1.2.4反