Struts2 学习笔记 1

一、功能:通过login.jsp页面登录,在result.jsp页面显示登录信息。

 

二、步骤:

1、下载struts2 http://struts.apache.org/download.cgi#struts210 ,使用的版本是:struts-2.0.14,解压缩。

2、在eclipse下建立 Dynamic Web Project

3、将 commons-logging-1.0.4.jar、freemarker-2.3.8.jar、ognl-2.6.11.jar、struts2-core-2.0.14.jar、xwork-2.0.7.jar 包拷贝到 WEB-INF/lib 目录下。

4、在WebContent目录下建立 login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struts 登录</title>
</head>
<body>

<form action="login.action" method="post">
	<div>
		<label>用户名:</label>
		<input type="text" name="userName">
	</div>
	<div>
		<label>密码:</label>
		<input type="password" name="password">
	</div>
	<div><input type="submit" value="登录"></input></div>
</form>
</body>
</html>

 

5、配置 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>MyStruts</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

 6、在 src目录下建立包 wuyechun.mystruts.action ,新建文件 LoginAction.java 。

package wuyechun.mystruts.action;
public class LoginAction {
	
	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;
	}
	
	public String execute() throws Exception{
		return "success";
	}

}

 

7、在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="strut2" extends="struts-default">
   			<action name="login" class="wuyechun.mystruts.action.LoginAction">
				<!--<result>/result.jsp</result> -->  			
				 <result name="success">/result.jsp</result> 
   			</action>
   		</package>
   </struts>

 

8、在WebContent目录下建立 result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录结果</title>
</head>
<body>
	<div><label>用户名:</label>${requestScope.userName}</div>
	<div><label>密码:</label>${requestScope.password}</div>
</body>
</html>

 

三、注意:

1、<form action="login.action" method="post"> 和

    <action name="login" class="wuyechun.mystruts.action.LoginAction">

对应关系。

 

四、登录显示登录结果。

 

附:

 

  <package name="login" namespace="/login" extends="default">
        <!-- 系统登录 -->
        <action name="findLogin" method="findLogin"  class="ustcsoft.hrlogin.st.HrLoginAction">
                 <result name="success">/FrameSet/Main.jsp</result>
		 <result name="input" >/index.jsp</result>    
		 <result name="error">/error.jsp</result>
        </action>
        

        <!-- 当前登录人员的功能菜单  -->
         <action name="findQtyModule" method="findQtyModule" class="ustcsoft.hrlogin.st.HrLoginAction"></action>

         <action name="loginOut" method="loginOut" class="ustcsoft.hrlogin.st.HrLoginAction">
	          <result name="success" >/index.jsp</result>
	          <result name="error">/error.jsp</result>
         </action>
   </package>

 

 

public class HrLoginAction extends AbstractAction {

        //登录
     	public String findLogin() {

               //用户名以及密码验证
                 if(!isCheckd){
                     return INPUT;
                  }               
                // String com.opensymphony.xwork2.Action.SUCCESS = "success"
                return SUCCESS;  
            }
       
   
 
       

          //获取用户模块
           public void findQtyModule() {
 
 

          }

       
       

           //退出
           public String loginOut() {

           }
 
 

     

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值