zoukankan      html  css  js  c++  java
  • Struts2 程序步骤

    1. 新建一个web project,

    手动导入包:

        D:Javajarstruts-2.3.24.1appsstruts2-blankWEB-INFlib copy到 WEB-INF/lib下

      D:Javajarstruts-2.3.24.1appsstruts2-blankWEB-INFsrcjava下的struts.xml copy到src下进行修改:

    <?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>
    
       
        <constant name="struts.devMode" value="true" />
    
        <package name="default" namespace="/" extends="struts-default">
    
            <default-action-ref name="index" />
    
            <global-results>
                <result name="error">/WEB-INF/jsp/error.jsp</result>
            </global-results>
    
            <global-exception-mappings>
                <exception-mapping exception="java.lang.Exception" result="error"/>
            </global-exception-mappings>
    
            <action name="login" class="net.nw.action.LoginAction">
                <result name="login_success">login_success.jsp</result>
                <result name="login_failure">login_failure.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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_1900_OGNL</display-name>
    
      <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>
        
       <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    

    Myeclipse配置的话:

    右键项目->myeclipse->add struts...-> 下面选择*.action, *.do, /*, 选择最后一个

    然后下一步选择core那个包, 会自动配置struts.xml和web.xml

    2. WebRoot下新建一个login.jsp:

    <%@ page language="java" import="java.util.*"%>
    <%@ page contentType="text/html; charset=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>My JSP 'login.jsp' starting page</title>
        
    	<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>
        <center>
        	<h1>系统登陆</h1>
        	<hr>
        	<form name="loginForm" action="<%=path%>/LoginServlet" method="post">
        		用户名: <input type="text" name="username"/><br>
        		   密码 : <input type ="text" name="password"/><br>
        		<input type="submit" value="登陆"/><br>
        	</form>
        </center>
      </body>
    </html>
    

    3. 新建登录成功页面和失败页面 login_success.jsp, login_failure.jsp

    <%@ page language="java" import="java.util.*"%>
    <%@ page contentType="text/html; charset=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>My JSP 'login.jsp' starting page</title>
        
    	<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>
        <center>
        	<h1>登陆成功</h1>
        	<hr>
        	
        </center>
      </body>
    </html>
    
    <%@ page language="java" import="java.util.*"%>
    <%@ page contentType="text/html; charset=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>My JSP 'login.jsp' starting page</title>
        
    	<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>
        <center>
        	<h1>登陆失败</h1>
        	<a href="<%=path%>/login.jsp">返回</a>
        	<hr>
        	
        </center>
      </body>
    </html>
    

    4. 视图层完毕, 下面是模型层: vo, dao,action

    vo层: new package: net.nw.vo,

    new class:Users.java:

    package net.nw.vo;
    
    public class Users {
    	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;
    	}
    }
    

    5. dao类:

    new package: net.nw.dao,

    dao类: UsersDAO.java:

    package net.nw.dao;
    
    import net.nw.vo.Users;
    
    public class UsersDAO {
    	public boolean usersLogin(Users u){
    		if(u.getUsername().equals("admin")&&u.getPassword().equals("123456")){
    			return true;
    		}
    		else{
    			return false;
    		}
    	}
    }
    

    6. action

    vo层: new package: net.nw.action

    new class:LoginAction

    package net.nw.action;
    
    import net.nw.dao.UsersDAO;
    import net.nw.vo.Users;
    
    public class LoginAction {
    	public String username;
    	public 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;
    	}
    
    	 public String execute(){
    		 Users u=new Users();
    		 u.setUsername(this.getUsername());
    		 u.setPassword(this.getPassword());
    		 UsersDAO dao=new UsersDAO();
    		 if(dao.usersLogin(u)){
    			 return "login_success";
    		 }
    		 else{
    			 return "login_failure";
    		 }
    	 }
    }
    

    配置struts如第一步. 

  • 相关阅读:
    Constants and Variables
    随想
    C#基础篇之语言和框架介绍
    Python基础19 实例方法 类方法 静态方法 私有变量 私有方法 属性
    Python基础18 实例变量 类变量 构造方法
    Python基础17 嵌套函数 函数类型和Lambda表达式 三大基础函数 filter() map() reduce()
    Python基础16 函数返回值 作用区域 生成器
    Python基础11 List插入,删除,替换和其他常用方法 insert() remove() pop() reverse() copy() clear() index() count()
    Python基础15 函数的定义 使用关键字参数调用 参数默认值 可变参数
    Python基础14 字典的创建修改访问和遍历 popitem() keys() values() items()
  • 原文地址:https://www.cnblogs.com/wujixing/p/5180171.html
Copyright © 2011-2022 走看看