struts2入门实例
来源:互联网 发布:政治经济类杂志 知乎 编辑:程序博客网 时间:2024/05/12 09:49
在MyEclipse下创建一个Web Project,名为Struts2_1。在项目名称上右键MyEclipse->Project Facets->Install Apache Facets(Struts2) ,则可添加struts2,会在src目录下生成 struts.xml文件。
在src目录下创建class类LoginAction.java,包路径为com.study.struts.action,代码如下:
package com.study.struts.action;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport { private String account; private String password; public String execute() { if ("abc".equals(account) && "123".equals(password)) { return SUCCESS; } return ERROR; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
在WebRoot下创建login.jsp,success.jsp和error.jsp
login.jsp文件如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="struts" %><%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>My JSP 'login.jsp' starting page</title> <%-- <struts:head theme="ajax" /> --%> <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> <struts:form action="loginPerson"> <struts:label value="登录系统"/> <struts:textfield name="account" label="账号"/> <struts:password name="password" label="密码"/> <struts:submit value="登陆"/> </struts:form> </body></html>
success.jsp 和error.jsp分别显示登陆成功和登陆失败
struts.xml文件如下:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts> <package name="main" extends="struts-default"> <global-results> <result name="login">/login.jsp</result> </global-results> <action name="loginPerson" class="com.study.struts.action.LoginAction"> <result name="success">/welcome.jsp</result> <result name="error">/error.jsp</result> </action> </package></struts>
web.xml文件如下:
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts_1</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> <!-- 截获所有的url --> </filter-mapping></web-app>
启动tomcat服务器,并部署struts2_1应用,就可以在浏览器输入http://localhost:8080/Struts_1访问,首先会进入登录页面,登陆成功则跳转到success.jsp页面显示欢迎,失败则跳转到error.jsp页面,显示用户名或密码错误。
0 0
- Struts2入门实例
- Struts2 入门实例代码
- Struts2 入门实例
- Struts2入门实例
- Struts2入门经典实例
- Struts2的入门实例
- Struts2简单入门实例
- Struts2入门经典实例
- Struts2开发入门实例
- Struts2入门实例
- struts2入门实例
- SSH-struts2入门实例
- struts2 helloworld入门实例
- struts2入门实例
- Struts2入门实例
- Struts2 入门实例
- struts2.x入门实例
- struts2入门实例
- 自定义Drawble-Xml
- 数倍提高工作效率的 Android Studio 赢巧奇技
- android的各种权限,请记得在androidmanifest.xml中声明!
- 黑马52期学后总结笔记(三)
- 前端资源汇总
- struts2入门实例
- HTTP 的重定向301,302,303,307(转)
- ffmpeg.exe 命令行视频处理图像质量
- html页面引用js路径问题解决
- mysql查询上一条和下一条信息
- 王爽<汇编语言>实验14 访问CMOS RAM 解答(详细注释)
- 打包proto文件以及protobuffers安装
- 如何优雅地使用 Sublime Text
- Android6.0动态获取权限