码迷,mamicode.com
首页 > 其他好文 > 详细

struts1

时间:2015-08-04 12:37:43      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

1.简单应用示例

导入struts1的jar包,然后配置xml,写java和jsp

/struts/WebRoot/Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="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 ‘index.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">
  </head>
  
  <body>
    <form action ="<%=request.getContextPath()%>/login.do">
        username : <input type=‘text‘ name =‘username‘ />
        password : <input type=‘password‘ name =‘password‘ />
        <input type="submit" value="login"/>
    </form>
  </body>
</html>

/struts/WebRoot/LoginSuccess.jsp

<%@ page language="java" import="java.util.*" pageEncoding="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 ‘index.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>
       登陆成功
  </body>
</html>

/struts/WebRoot/LoginFailure.jsp

技术分享
<%@ page language="java" import="java.util.*" pageEncoding="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 ‘index.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>
    登陆失败
  </body>
</html>
View Code

/struts/WebRoot/WEB-INF/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">
  <servlet>
      <servlet-name>action</servlet-name>
      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
      <init-param>
          <param-name>config</param-name>
          <param-value>
          /WEB-INF/struts-config.xml
          </param-value>
      </init-param>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>action</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

/struts/WebRoot/WEB-INF/struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
    <form-beans>
        <form-bean name="loginForm" type="cn.itcast.LoginForm"></form-bean>
    </form-beans>
    <action-mappings>
        <action path="/login" type="cn.itcast.LoginAction" name="loginForm">
            <forward name="success" path="/LoginSuccess.jsp"></forward>
            <forward name="failure" path="/LoginFailure.jsp"></forward>
        </action>
    </action-mappings>
</struts-config>

/struts/src/cn/itcast/LoginAction.java

package cn.itcast;

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 {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // TODO Auto-generated method stub
        LoginForm loginForm= (LoginForm)form ;
        String username = loginForm.getUsername();
        if("itcast".equals(username)){
            return mapping.findForward("success");
        }else{
            return mapping.findForward("failure");
        }
    }

}

/struts/src/cn/itcast/LoginForm.java

package cn.itcast;

import org.apache.struts.action.ActionForm;

public class LoginForm extends ActionForm {
    private String username = null;
    private String password = null;
    
    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;
    }
    
}

2.Action什么时候初始化?发出该Action请求,不是在读取配置文件时初始化。

3.每个Action只会初始化一次

4. Action是线程不安全的,因为所有的请求都共享一个action实例

5.怎样实现action的安全性编程?

  1)注意不要用实例变量或者类变量共享只是针对某个请求的数据

  2)注意资源操作的同步性

应用:统计一个action的访问次数

在调用时加1,注意用Sychronzied同步变量

6.ActionMapping类,可以获取ActionMapping里配置的信息

 

struts1

标签:

原文地址:http://www.cnblogs.com/fanglove/p/4701324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!