您的位置:首页 > 编程语言 > Java开发

Struts2 Action(2)

2015-06-10 00:00 183 查看
摘要: public class LoginAction3 extends ActionSupport

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2_01_start</display-name>

<!-- step1:导架包 -->
<!-- step2:filter  用来启动struct2框架 -->
<filter>
<filter-name>structs2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>structs2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<!-- step3:struct2 的核心配置文件 -->
<!-- step4:添加项目所需的文件 -->
<struts>
<!-- 请求消息的编码方式  默认的编码为UTF-8 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 -->
<constant name="struts.action.extension" value="action,do,go,zhangsan,lisi"></constant>
<!-- 默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开
-->
<constant name="struts.devMode" value="false"></constant>
<!-- 启用Action的name是否支持斜线(/) -->
<constant name="struts.enable.SlashesInActionNames" value="true"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

<package name="default" namespace="/" extends="struts-default">

<!-- Action 在Struts2中Action是用来处理请求业务的 -->
<action name="login" class="derun.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="fail">/fail.jsp</result>
</action>

<action name="login2" class="derun.action.LoginAction2">
<result name="success">/success.jsp</result>
<result name="error">/fail.jsp</result>
</action>

<action name="login3" class="derun.action.LoginAction3">
<result name="success">/success.jsp</result>
<result name="error">/fail.jsp</result>
</action>
</package>

</struts>


package derun.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction3 extends ActionSupport {

private String userName;
private String password;

@Override
public String execute() throws Exception {
if(userName.equals("admire")&&password.equals("admire")){
return SUCCESS;
}else{
return ERROR;
}
}

public void setUserName(String userName) {
this.userName = userName;
}

public void setPassword(String password) {
this.password = password;
}

}


<%@ 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>Struts2 HelloWorld示例</title>

</head>

<body>
<h3>Struts2 HelloWorld示例</h3>
<hr>
<form action="login3.action" method="post">
用户名:<input type="text" name="userName"><br>
密    码:<input type="password" name="password"><br>
<input type="submit" value="登录">  
<input type="reset" value="重置">
</form>
</body>
</html>


<%@ 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>

</head>

<body>
<h3>登录成功</h3>
</body>
</html>


<%@ 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>

</head>

<body>
<h3>登录失败</h3>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: