Struts 1.3(第一例) - Login

本想跳过直接学Struts 2的,想想,还是先学Struts 1,万一到时去那个公司,人家用的是1,那还是要学,以及了解下1与2的区别在哪里。

上例子,很简单的一个网上login例子,再思考下Struts想干嘛。

Struts下载:

http://struts.apache.org/download.cgi#struts23161


先建三个jsp文件:

1)login.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
    <form action="login.do" method="post">
        Username:<input type="text" id="username" name="username" /><br/>
        Password:<input type="password" id="password" name="password"/><br/>
        <input type="submit" value="Login" />
    </form>
</body>
</html>

 

2)main.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
    Welcome:${ username }
</body>
</html>

 

3)error.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
    Error
</body>
</html>

 

新建一个bean:UserForm,这个bean是Form Bean,对应的是login.jsp里头form中的控件name:

package com.my.forms;

import org.apache.struts.action.ActionForm;

public class UserForm extends ActionForm {
    public UserForm() {}
    
    private String username;
    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;
    }

    private String password;
}

 

新建 一个Action:LoginAction,这个action需要重写execute(...)方法,其中,参数form为bean form(Struts自动匹配)

package com.my.actions;

import javax.servlet.http.*;

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.my.forms.*;

public class LoginAction extends Action {
    public LoginAction() {
    }

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 将Form强转为UserForm
        UserForm userForm = (UserForm) form;
        String username = userForm.getUsername();
        String password = userForm.getPassword();
        // 将用户名存入request表单域中
        request.setAttribute("username", username);
        if ("123".equals(password))
            return mapping.findForward("main");
        else
            return mapping.findForward("error");
    }
}

 

在/WEB-INF中新建一个xml文件: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.my.forms.UserForm"></form-bean>
    </form-beans>
    <action-mappings>
        <action path="/login" name="userForm" scope="request" type="com.my.actions.LoginAction">
            <forward name="main" path="/main.jsp"></forward>
            <forward name="error" path="/error.jsp"></forward>
        </action>
    </action-mappings>
</struts-config>

 

在web.xml中加入servlet的配置:

    <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>

 

完成。


 

posted @ 2014-03-30 21:58  横渡  阅读(1722)  评论(0编辑  收藏  举报