[web工程]集成struts2 mvc

返回:[web工程]Struts2+Spring3.1+Hibernate3.6的集成

上一步:[web工程]创建web工程

下一步:[web工程]集成spring

集成struts2 mvc

  1. 添加jee5(demo中的third-lib/jee5目录中)的jar到java build path中
  2. 复制struts2的jar包到lib目录,并添加这些jar到java build path中
    struts2(下载)的包依赖关系如:
    =============================================
    struts2 lib: v2.2.3
    	struts2-core-2.2.3.jar
    	xwork-core-2.2.3.jar
    	ognl-3.0.1.jar
    	
    	depend on libs:
    		commons-io-2.0.1.jar
    		commons-lang-2.5.jar
    		freemarker-2.3.16
    =============================================
     
    最终java build path如图:


     
  3. 在目录WEB-INF/jsp中创建登录页面login.jsp和welcome.jsp


    login.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>系统登录:</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="loginAction!check.do" method="post">
    			<table align="center">
    					<tr><td>用户名:</td><td><input name="userName" value="${userName }"></td></tr>
    					<tr><td>密&nbsp;码:</td><td><input name="password" type="password"></td></tr>
    					<tr><td><input type="submit" value="登录"></td><td><input type="reset" value="重置"></td></tr>
    			</table>
    		</form>
    	</body>
    </html>
    
     
    welcome.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>系统首页</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">
    	</head>
    
    	<body>
    		<center>
    			<h1>欢迎您:${userName }!</h1>
    		</center>
    	</body>
    </html>
    
     
  4. 创建登录验证action, LoginAction
    /**
     * Copyright (c) 2010-2020 by wasion.com
     * All rights reserved.
     * @author <a href="mailto:[email protected]">skzr.org</a>
     * @date 2011-5-19 下午10:20:57
     */
    package org.skzr.demo.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    /**
     * 登录检测
     * @author <a href="mailto:[email protected]">skzr.org</a>
     * @version 1.0.0
     * @since JDK1.6
     */
    public class LoginAction extends ActionSupport {
    	private static final long serialVersionUID = 1L;
    	/** 用户名 */
    	private String userName;
    	/** 密码 */
    	private String password;
    	
    	/**
    	 * @return {@link #userName}
    	 */
    	public String getUserName() {
    		return userName;
    	}
    	/**
    	 * @param userName {@link #userName}
    	 */
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    	/**
    	 * @return {@link #password}
    	 */
    	public String getPassword() {
    		return password;
    	}
    	/**
    	 * @param password {@link #password}
    	 */
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    	/**
    	 * 登录验证
    	 * @return 验证后页面视图
    	 */
    	public String check() {
    		return "admin".equals(userName) ? "welcome" : "success";
    	}
    }
    
     
  5. 修改web.xml文件,增加struts2拦截器配置
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    	<description>我爱编程</description>
    	<display-name>我爱编程</display-name>
    	
    	<filter>
    		<filter-name>struts2</filter-name>
    		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    		<init-param>
    			<param-name>config</param-name>
    			<param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
    		</init-param>
    	</filter>
    	<filter-mapping>
    		<filter-name>struts2</filter-name>
    		<url-pattern>*.do</url-pattern>
    	</filter-mapping>
    	
    	<welcome-file-list>
    		<welcome-file>index.jsp</welcome-file>
    	</welcome-file-list>
    </web-app>
    
     
  6. 创建struts.xml文件,并配置LoginAction
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
    	<package name="demo" extends="struts-default" namespace="/">
    		<action name="loginAction" class="org.skzr.demo.action.LoginAction">
    			<result>/WEB-INF/jsp/login.jsp</result>
    			<result name="welcome">/WEB-INF/jsp/welcome.jsp</result>
    		</action>
    	</package>
    </struts>    
    
     struts.properties文件配置为:
    ##这个根本不能够使用 struts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml,cfg/struts/struts-cems.xml
    #struts.i18n.encoding=UTF-8
    #指定Struts2处于开发状态
    struts.devMode=false
    struts.i18n.reload=false
    #指定当Struts2配置文件改变后,web框架是否重新加载Struts2的配置文件
    struts.configuration.xml.reload=false
    #是不是允许actionName!MethodName.action的调用形式
    #struts.enable.DynamicMethodInvocation=true
    #struts.custom.i18n.resources=globalMessages
    #配置服务器运行的端口号(所有生成的URL都会被正确创建)
    #struts.url.http.port = 8080
    #struts.serve.static.browserCache=false
    #struts.custom.i18n.resources=i18n
    #struts.ui.templateDir=/WEB-INF/template
    ### struts标签使用哪种默认样式
    #struts.ui.theme=simple
    
    ### struts标签的模板所在路径
    #struts.ui.templateDir=template
    
    ## struts拦截的名称,防止出现问题
    struts.action.extension=do
    #struts.objectFactory=spring
    
    #struts.multipart.saveDir=temp/
    #struts.multipart.maxSize=10485760
    #struts.enable.SlashesInActionNames=true
     
  7. 运行主页看看效果
    1. 登录页面:


       
    2. 登录成功后:


       下载demo包

猜你喜欢

转载自skzr-org.iteye.com/blog/1050930