struts2 入门

来源:互联网 发布:淘宝福建拍卖 编辑:程序博客网 时间:2024/04/30 01:14

Apache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications. The framework is designed to streamline the full development cycle, from building, to deploying, to maintaining applications over time.

Apache Struts 2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts 2. This new version of Struts is simpler to use and closer to how Struts was always meant to be.

这是Struts 2的官方介绍。大意是:

Apache的Struts 2是一个优雅的,可扩展的框架用于创建企业级的Java Web应用程序。该框架旨在简化整个开发周期,从开发,部署,一直到维护这个应用程序。

Apache Struts 2的原型是WebWork 2。独立工作了几年后,WebWork和Struts的社区联手创建了Struts2。这个新版本的Struts是更简单易用的和....


开发步骤是:

1.导入jar包,主要应包括这么几个包

  -commons-logging-1.1.jar

  -freemarker-2.3.13.jar

  -ognl-2.6.11.jar

  -struts2-core-2.1.6.jar

  -xwork-2.1.2.jar

  -commons-io-1.3.2.jar (可选)

 -commons-fileupload-1.2.1.jar (可选)


2.配置web.xml

 struts 2官方给的Example:

<web-app id="WebApp_9" 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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>        <init-param>        <param-name>actionPackages</param-name>        <param-value>com.mycompany.myapp.actions</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!-- ... --></web-app>

这里要注意的是,以上是针对 Struts 2版本>=2.1.3,如果是以下的,那么就要这么配置过滤器

 <filter>        <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>    ...
另外,还可以配置一个标签库

  <!-- ... -->    </welcome-file-list>    <taglib>       <taglib-uri>/s</taglib-uri>       <taglib-location>/WEB-INF/struts-tags.tld</taglib-location>    </taglib></web-app>


3.在src下增加struts.xml,该文件编译后,会出现在WEB-INF下的classes文件夹下

namespace 命名空间要注意

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!--     <constant name="struts.enable.DynamicMethodInvocation" value="false" />    <constant name="struts.devMode" value="false" />    <include file="example.xml"/> -->       <package name="default" namespace="/" extends="struts-default">        <action name="login" class="org.zhe.mine.action.LoginAction"  >        <result name="success">/success.htm</result>        <result name="input">/index.jsp</result>        </action>         </package></struts>


4.index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><%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><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"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>  <center><p>登陆评价系统</p>    <s:form action="login" namespace="/" validate="true">    <s:textfield label="专家ID" name="ID"/>    <s:password label="专家密码" name="password"/>    <s:submit value="登录" />    </s:form>    <FONT color="red"><s:actionerror /></FONT>  </center>  </body></html>


5.Expert.java

package org.zhe.mine.model;public class Expert {private String expertID;private String password;public String getExpertID() {return expertID;}public void setExpertID(String expertID) {this.expertID = expertID;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}


6.LoginAction.java
package org.zhe.mine.action;import org.zhe.mine.model.Expert;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;/** * LoginAction  * ModelDriven * @author zhe * */public class LoginAction extends ActionSupport implements ModelDriven<Expert>{private Expert expert = new Expert();LoadProp loadProp = new LoadProp();public Expert getModel() {return expert;}public String execute() throws Exception{String expertID = expert.getExpertID();String password = expert.getPassword();if(expertID.equals("1") && password.equals("2")) {return SUCCESS;} else{this.addActionError("用户名或密码错误"); //对应jsp页面的<s:actionerror />return INPUT;}}}


7.LoginAction-validation.xml

 校验jsp页面输入正确与否的xml,命名规则:  xxAction-validation.xml  放在与xxAction相同目录下,注意jsp页面加上 validate="true"

 <s:form action="login" namespace="/" validate="true">

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE validators PUBLIC           "-//OpenSymphony Group//XWork Validator 1.0//EN"           "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">      <validators><validator type="requiredstring"><param name="fieldName">expertID</param><message>专家号不能为空</message></validator><validator type="requiredstring"><param name="fieldName">password</param><message>密码不能为空</message></validator></validators>







原创粉丝点击