day8:struts2小demo,实现一个用户登录模块

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

      
      <welcome-file-list>
        <welcome-file>Index.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>
    </filter-mapping>

    
</web-app>

 

struts.xml:

<?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">

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="loginA" class="LoginAction">
            <result name="success">
               /Welcome.jsp
            </result>
            <result name="login">
               /Index.jsp
            </result>
        </action>
    </package>
    
</struts>

action:

import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends    ActionSupport{
    private    String    username;
    private    String    password;
    /**
     * @return username
     */
    public String getUsername() {
        return username;
    }
    /**
     * @param username 要设置的 username
     */
    public void setUsername(String username) {
        this.username = username;
    }
    /**
     * @return password
     */
    public String getPassword() {
        return password;
    }
    /**
     * @param password 要设置的 password
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
    public    String    execute(){
        System.out.println("方法开始执行");
        if( "xx".equalsIgnoreCase(username)&&"xxsoft".equals(password) ){
            System.out.println("方法执行结束");
            return    SUCCESS;
        }
        else
            return    LOGIN;
            
    }

}

Index.jsp:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>???????</title>
</head>
<body>
    <s:form    action="loginA">
        <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:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>???????</title>
</head>
<body>
    登录成功,欢迎您<s:property value="username"></s:property>
</body>
</html>

 

 

弱弱的有两个小问题:

1,启动运行后每次都要修改url地址,可有什么办法?

2,tomcat启动中可否对程序做修改后刷新页面查看结果?

posted on 2016-09-22 15:38  ddouble  阅读(116)  评论(0)    收藏  举报