`
futrueboy
  • 浏览: 83747 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

struts实现登录

阅读更多
Struts编程
使用Struts框架编写简单Web应用程序。
(1) 使用Struts的<html>标记编写简单的用户登录表单。
(2) 创建与表单对应的ActionForm类。
(3) 创建Action类:接收传入的ActionForm对象,并验证其中的用户名和密码是否正确;若正确,转至登录成功页面success.jsp,反之转至登录失败页面fail.jsp。
(4) 编写success.jsp和fail.jsp,显示简单的成功或失败信息。
(5) 配置struts-config.xml文件,完成上述Web应用组件的装配。
(6) 说明应用程序的工作原理。
(7) 实验报告中附上以下内容的源码:
     a) 使用Struts标记库完成的表单。
     b) ActionForm类。
     c) Action类。
     d) struts-config.xml文件中你添加的配置内容。
1.原理:
使用struts的<html>标记编写用户表单,默认情况下,表单中的字段会自动与ActionForm中的属性关联起来
		<html:form action = "/loginAction.do" method = "post">
			username <html:text property = "username"/><br/>
			password <html:password property = "password"/><br/>
			<html:submit property = "submit" value = "OK"/>
		</html:form>

在这边,username和password字段会自动与ActionForm的继承类LoginForm属性关联
<form-beans>
		<form-bean name="loginForm" type="form.LoginForm"/> 
    </form-beans>
   

以下是对提交数据后,系统做的映射
    <action-mappings>
        <action path="/loginAction"  type = "action.LoginAction"  name = "loginForm" input = "/login.jsp">
			<forward name = "failure" path = "/failure.jsp"/>
			<forward name = "success" path = "/success.jsp"/>
		</action>
    </action-mappings>

path属性指定了访问Action的路径
type指出Action关联到的类
name指出要传给Action的类ActionForm
input表示表单登录失败时要导向的页面

以下直接使用struts-blank-1.3.10编写登录程序
login.jsp
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:html>
	<head></head>
	<body>
		<html:form action = "/loginAction.do" method = "post">
			username <htm l:text property = "username"/><br/>
			password <html:password property = "password"/><br/>
			<html:submit property = "submit" value = "OK"/>
		</html:form>
	</body>
</html:html>


success.jsp
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:html>
	<head></head>
	<body>
		welcome, <%= request.getParameter("username") %>!
	</body>
</html:html>


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

    <form-beans>
		<form-bean name="loginForm" type="form.LoginForm"/> 
    </form-beans>

<!-- =========================================== Action Mapping Definitions -->

    <action-mappings>
        <action path="/loginAction"  type = "action.LoginAction"  name = "loginForm" input = "/login.jsp">
			<forward name = "failure" path = "/failure.jsp"/>
			<forward name = "success" path = "/success.jsp"/>
		</action>
    </action-mappings>


<!-- ======================================== Message Resources Definitions -->

    <message-resources parameter="MessageResources" />
</struts-config>

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

  <!DOCTYPE web-app PUBLIC
	"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
	"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
  <display-name>Struts Blank Application</display-name>
  
  <!-- 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>login.jsp</welcome-file>
  </welcome-file-list>

</web-app>


  • 描述: 显示结果
  • 大小: 26.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics