`
cakin24
  • 浏览: 1333118 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

使用工具半自动化开发struts

阅读更多
一 使用工具开发struts有两种方式
1 用工具引入struts包和配置文件,然后自己配置struts-config.xml,完成开发。或称半自动化。
2 完全依赖myeclipse提供的工具完成struts开发。或称全自动化。
 
二 实例——半自动化实现登录验证系统
1 使用工具建立web工程
 
2 导入struts开发包


 


 
自动引入struts开发包,自动生成struts-config.xml以及web.xml,并完成一些配置。
 
3 开发login.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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">
  </head>
 
  <body>
    <form action="/strutstool1/login.do" method="post">
        u:<input type="text" name="username"><br/>
        p:<input type="password" name="password"><br/>
        <input type="submit" value="login">
    </form>
  </body>
</html>
 
4 开发action和actionForm
LoginAction代码
package com.cakin.actions;
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;
import com.cakin.forms.UserForm;
public class LoginAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        //把form转成对应的UserForm对象
        UserForm userForm=(UserForm)form;
        
        if("123".equals(userForm.getPassword())){
            //把名字存放到request对象域
            //request.setAttribute("username", userForm.getUsername());
            return mapping.findForward("ok");
        }
        else{
            return mapping.findForward("err");
        }
    }
}
UserForm代码
package com.cakin.forms;
 
import org.apache.struts.action.ActionForm;
 
public class UserForm extends ActionForm {
        private String username;
        private String password;
        public String getUsername() {
                return username;
        }
        public void setUsername(String username) {
                System.out.println("actionservlet调用,传入用户"+username);
                this.username = username;
        }
        public String getPassword() {
                return password;
        }
        public void setPassword(String password) {
                this.password = password;
        }
}
 
5 手动配置struts-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd";>
 
<struts-config>
  <form-beans>
        <form-bean name="userForm"  type="com.cakin.forms.UserForm"></form-bean>
  </form-beans>
 
  <global-exceptions />
  <global-forwards />
  <action-mappings>
        <action path="/login" name="userForm" type="com.cakin.actions.LoginAction">
                <forward name="ok" path="/WEB-INF/ok.jsp"></forward>
                <forward name="err" path="/WEB-INF/err.jsp"></forward>
        </action>
  </action-mappings>
  <message-resources parameter="com.cakin.struts.ApplicationResources" />
</struts-config>
 
6 开发ok.jsp和err.jsp
<%@ page language="java" import="java.util.*" import="com.cakin.forms.*" 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 'wel.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>
    welcome <%=((UserForm)session.getAttribute("userForm")).getUsername() %> <br>
    <a href="/strutslogin/">返回重新登录</a>
  </body>
</html>
 
7 测试


 
 
8 上面的开发过程,可以提高我们的开发效率
  • 大小: 51.4 KB
  • 大小: 31.2 KB
  • 大小: 19.4 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics