Eclipse配置struts1.1,实现简单登录

来源:互联网 发布:域名注册流程图 编辑:程序博客网 时间:2024/05/23 15:16

新建Java Project,新建WebContent及其子文件夹,形成如图所示的结构: 

 

注意把你下载到的struts的jar包全部复制到lib文件夹里面。

 

右键点击工程,选择属性,选择Java Build Path,在Source标签中将Default output folder 改为Login/WebContent/WEB-INF/classes,如图:

 

 

 

 

 

然后再Libraries标签中点击Add Library,添加1.5的JRE System Library和tomcat的Server Runtime,还有User Library,在User Library中新建一个local struts library,然后在该library种添加lib文件夹下struts的 jar包。如此一来,你的工程就会含有以下三个library:

 

 

 

 

 

 

在WEB-INF文件夹下的struts-config.xml和web.xml中分别按下文所示添加内容。

 

struts-config.xml

 

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!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="loginForm" type="login.LoginActionForm" />

    </form-beans>

    <action-mappings>

       <action path="/login" type="login.LoginAction" name="loginForm"

           scope="request">

           <forward name="success" path="/login_success.jsp" />

           <forward name="error" path="/login_error.jsp" />

       </action>

    </action-mappings>

</struts-config>

 

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <!-- Standard Action Servlet Configuration -->

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

       <load-on-startup>2</load-on-startup>

    </servlet>

  <!-- Standard Action Servlet Mapping -->

    <servlet-mapping>

       <servlet-name>action</servlet-name>

       <url-pattern>*.do</url-pattern>

    </servlet-mapping>

  <!-- The Usual Welcome File List -->

    <welcome-file-list>

       <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

</web-app>

 

然后在WebContent文件夹下添加四个jsp文件:

 

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

</head>

<body>

<a href="login.jsp">登录</a>

<br>

</body>

</html>

 

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

登录失败

</body>

</html>

 

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

<%=request.getAttribute("username")%>,登录成功

</body>

</html>

 

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

</head>

<body>

<h1>用户登录</h1>

<hr>

<form action="login.do" method="post">用户:<input type="text"

    name="username"><br>

密码:<input type="password" name="password"><br>

<input type="submit" value="登录"></form>

</body>

</html>

 

然后在server perspective中新建一个server,可选择5.5或6.0的tomcat,然后双击该server,进入属性设置,点击左下角的modules按钮,如下图所示:

 

 

 

 

 

 

然后点击Add External Web Module,选择你工程文件的WebContent文件夹,然后在path框中输入login,如下图所示:

 

 

 

 

 

启动tomcat,打开浏览器,输入http://localhost:8080/login,即可访问该struts工程。

 

使用Eclipse构建struts工程比使用MyEclipse要复杂很多,不过Eclipse的启动和运行速度也远优于MyEclipse,所以大家可以根据自己电脑的配置和工作需要来选择IDE。

原创粉丝点击