Strus1.x配置

  1. 创建web工程
    2.添加strutslib库jar文件
    3.创建struts配置文件struts-config.xml
    4.在web.xml中配置struts-config配置文件及配置actionservlet
    '''

    action
    org.apache.struts.action.ActionServlet

    config
    /WEB-INF/struts-config.xml

    2


action
*.do


''
5.配置struts-config文件在配置文件中同时声明forward跳转页面配置及form关联配置

'''


    

 
   
            
            
      


'''

实例代码如下:

1 前台代码

登录页面
'''
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>




My JSP 'index.jsp' starting page


    





This is my JSP page.


'''
成功页面
'''
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>




My JSP 'ok.jsp' starting page



    




登录成功!!



'''
失败页面

'''

登录失败!!



'''

2前台页面设计完毕后新建逻辑处理类,继承于Action并重写excute方法。

2.1首先配置struts-config文件正确配置forword和formbeans
配置示例如下:
'''

"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">


    

 
   
            
            
      

'''
login处理类代码实现
'''
package com.scm.auth.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class loginAction extends Action{

//重写执行函数
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // TODO Auto-generated method stub
    Forminfo lform =(Forminfo)form;
    String username = lform.getUsername();
    String passwd = lform.getPasswd();
    if(username != null && username.equals("admin")){
        return mapping.findForward("success");
    }else {
        return mapping.findForward("failure");
    }
    //return super.execute(mapping, form, request, response);
}

}
'''
form-bean类实现
'''
package com.scm.auth.action;

import org.apache.struts.action.ActionForm;

public class Forminfo extends ActionForm{
String username;
String passwd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}

}
'''

你可能感兴趣的:(Strus1.x配置)