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
原创粉丝点击