首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2+hibernate3+spring2读书笔记一(struts2 开发环境)

2012-10-15 
struts2+hibernate3+spring2读书笔记1(struts2 开发环境)最近买了谢星星(阿蜜果)的企业应用架构设计-Strut

struts2+hibernate3+spring2读书笔记1(struts2 开发环境)
最近买了谢星星(阿蜜果)的企业应用架构设计-Struts2+Hibernate3+Spring2,想更好地学习与管理资料,现在在博客上写读书笔记,呵呵!

                        第2篇  Struts2篇

  一.搭建Struts2的开发环境
     
      1.建立工程.
      2.添加Struts2的jar包(现暂需要的5个重要的jar包struts2-core-2.0.11.2.jar,xwork-2.05.jar,ognl-2.6.11.jar,freemarker-2.3.8.jar,commons-logging-1.0.4.jar)
      3.配置web.mxl
       

<?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"><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

      4.创建struts配置文件:struts.xml
       
        用eclipse创建的方法:选择工程的src目录后,单击右健选择New-Other命令,在弹出的窗口选择xml-xml,单击Next按钮后输入xml文件名:struts.xml,完成。
        创建完成后还要修改其内容,内容如下:
        
 <!DOCTYPE struts PUBLIC "-//Apache Sofeware Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><include file="struts-default.xml"/><package name="amigo" extends="struts-default"></package></struts>

  呵呵,到了这一步Struts 2的开发环境就完成了。


二.用Struts2实现用户登录(实例需求是当用户名和密码输入不正确时(都为amigo)),跳转到登录成功界面,相反就跳到失败页面。)

1.编写登录页面login.jsp
首先是在webroot下创建login.jsp页面,代码运用了struts2的标签,内容如下:
<%@ page contentType="text/html;chartset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %>  //引入标签库,名为S<html>  <head>  <meta http-equiv="content-type" content="text/html; charset=UTF-8">   <title>登录系统</title>  </head>  <body>    <div align="center">    ${requestScope.message }   //el表达式,把登陆成功与否的信息读出来。        <s:form action="login" method="post">   //struts2 的form标签    <s:textfield name="loginName" size="20" label="用户名" />   //struts2 的text标签    <s:password name="password" size="20" label="密码"/>    //struts2 的password标签    <s:submit value="提交"/>(struts2 的submit标签)    </s:form>                </div>  </body></html>
2.编写登陆成功页面success.jsp 具体内容如下:
   
<%@ page language="java" contentType="text/html; charset=UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>登录成功界面</title></head><body>登录成功!</body></html>
3.编写登陆Action LoginAction
   在src目录中新建包:amigo.struts.login.action,并创建loginAction类,此类继承com.opensymphony.xwork2.ActionSupport类。具体代码如下:

package amigo.struts.login.action;import com.opensymphony.xwork2.ActionSupport;/***用户登陆的action*/public class LoginAction extends ActionSupport {//用户名public String loginName;//密码public String password;//执行完返回的信息public String message;public String getLoginName() {return loginName;}public void setLoginName(String loginName) {this.loginName = loginName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}//执行用户的验证的方法public String execute() throws Exception{if("amigo".equals(loginName)&&"amigo".equals(password)){  message=loginName+"登录成功!";}else{  message=loginName+"登录失败!";  return INPUT;}      return SUCCESS;}}
4.修改Struts配置文件:struts.xml 具体内容如下:

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><include file="struts-default.xml"/><package name="amigo" extends="struts-default"><action name="login" pageEncoding="UTF-8"%>,问题解决。






     

热点排行