zoukankan      html  css  js  c++  java
  • Struts2使用demo

    创建一个web project;

    导入Struts2的jar包放到lib目录下:

    WEB-INF下面创建login.jsp和welcome.jsp;index.jsp删掉;

    说一下<%@ taglib prefix="s" uri="/struts-tags"%> 

    这句是Struts2标签引入,一定记得写 不然下面的表格就不认了!

    login.jsp页面代码:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>登录</title>
    </head>
    <body>
    <s:form action="login" method="post">
    <s:label value="系统登录"></s:label>
    <s:textfield name="username" label="账号"></s:textfield>
    <s:password name="password" label="密码"></s:password>
    <s:submit value="登录"></s:submit>
    </s:form>
    </body>
    </html>

    welcome.jsp页面代码:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>欢迎</title>
    </head>
    <body>
    欢迎${username}
    </body>
    </html>

    在src下创建struts.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    <package name="default" namespace="/" extends="struts-default">
    <action name="login" class="LoginAction" method="execute" >
    <result name="success">/WEB-INF/welcome.jsp</result>
    <result name="login">/WEB-INF/login.jsp</result>
    </action>
    </package>
    </struts>

    在src下编写LoginAction类:

    import com.opensymphony.xwork2.ActionSupport;
    public class LoginAction extends ActionSupport{
    private static final long serialVersionUID = 1L;
    //该类继承了ActionSupport,就可以直接使用SUCCESS LOGIN等变量和 重写execute方法
    private String username;
    private String password;
    public String getUsername() {
    return username;
    }
    public void setUsername(String username) {
    this.username = username;
    }
    public String getPassword() {
    return password;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    @Override
    public String execute() throws Exception {
    if("sxh".equals(username) && "123".equals(password))
    return SUCCESS;
    return LOGIN;
    }
    }

    编写web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
    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_2_5.xsd">
    <display-name></display-name>
    <welcome-file-list>
    <welcome-file>/WEB-INF/login.jsp</welcome-file>
    </welcome-file-list>

    <filter>
    <!--指定Struts2的核心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>
    </filter-mapping>
    </web-app>

     部署到tomcat启动就可以运行了!!!!!

    代码链接:http://pan.baidu.com/s/1bpED1gj

  • 相关阅读:
    Mysql优化与使用集锦
    用条件注释判断浏览器版本,解决兼容问题
    高效的使用 Response.Redirect
    JS中字符串的相关操作
    Http压力测试工具HttpTest4Net
    纯CSS(无 JavaScript)实现的响应式图像显示
    jquery使用jsonp进行跨域调用
    JS函数重载解决方案
    C# 实现将 PDF 转文本的功能
    iFrame的妙用
  • 原文地址:https://www.cnblogs.com/MissSu/p/6102733.html
Copyright © 2011-2022 走看看