Struts2 - Study 1

领略下传说中的Struts2,写了个小例子,有点意思。比起.net中的MVC有意思的在于它是你自己一步步去配置实现,想怎么搞就怎么搞,.net的MVC,它干了什么事你完全不知,只不过知道怎么用而已。

下载的struts2版本为2.3,里头有些小例子,我直接使用了最简单的那个struts2-blank.war里的jar包即可。(这是struts2最基本的jar,其实还有一些可以删除的,比如我删除了log4J)

1)有以下有个Jar需要引入到lib文件夹:

asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3..jar
commons-io-2.2.jar
commons-lang3-3.1.jar
commons-logging-1.1..jar
freemarker-2.3..jar
javassist-3.11..GA.jar
ognl-3.0..jar
struts2-core-2.3.16.1.jar
xwork-core-2.3.16.1.jar

2)建一个BaseActionSupport.java

package com.my.action;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport; /**
* Base Action class for the Tutorial package.
*/
public class BaseActionSupport extends ActionSupport { public String getContextPath() {
return ServletActionContext.getRequest().getContextPath();
} }

3)建一个LoginAction.java

package com.my.action;

public class LoginAction extends BaseActionSupport {

    private String userName;
private String password; public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
} public String execute() throws Exception {
return INPUT;
} public String signin() {
if( "admin".equals(getUserName()) && "admin".equals(getPassword()) ) {
return SUCCESS;
}
else {
return "false";
}
}
}

4)建一个MainAction.java

package com.my.action;

public class MainAction extends BaseActionSupport {

    public String execute() throws Exception {
return SUCCESS;
} }

5)在src目录下建一个struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results>
<result name="error">/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings> </package> <include file="struts_login.xml"/> <!-- Add packages here --> </struts>

6)在src目录下建一个struts_login.xml

<?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> <package name="login" namespace="/" extends="default"> <action name="login_*" class="com.my.action.LoginAction" method="{1}">
<result type="redirectAction" name="success">main</result>
<result type="redirect" name="false">login</result>
<result name="input">/pages/Login.jsp</result>
</action>
<action name="main" class="com.my.action.MainAction">
<result name="success">/pages/Main.jsp</result>
</action>
<action name="*" class="com.my.action.BaseActionSupport">
<result>/pages/{1}.jsp</result>
</action> </package>
</struts>

7)建一个jsp:Login.jsp

<%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <html>
<head>
</head>
<body>
<form action="login_signin" method="post">
User name:<input type="text" id="userName" name="userName" /><br/>
Password:<input type="password" id="password" name="password" /><br/>
<input type="submit" value="Login" />
</form>
<body>
</html>

8)建一个jsp:Main.jsp

<%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <html>
<head>
</head>
<body>
This is main page.<br/>
Context Path:${contextPath}<br/>
Action URL:<s:url action="main"/>
<body>
</html>

9)修改web.xml

    <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

因为没有用eclipse,直接使用命令行javac,写了一个bat做编译:build.bat

@echo off
set libDir=D:\Study\apache-tomcat-7.0.52\webapps\testStruts\WEB-INF\lib
set tempClassPath=%classpath%
set classpath=%libDir%\asm-3.3.jar;%classpath%
set classpath=%libDir%\asm-commons-3.3.jar;%classpath%
set classpath=%libDir%\asm-tree-3.3.jar;%classpath%
set classpath=%libDir%\commons-fileupload-1.3.1.jar;%classpath%
set classpath=%libDir%\commons-io-2.2.jar;%classpath%
set classpath=%libDir%\commons-lang3-3.1.jar;%classpath%
set classpath=%libDir%\commons-logging-1.1.3.jar;%classpath%
set classpath=%libDir%\freemarker-2.3.19.jar;%classpath%
set classpath=%libDir%\javassist-3.11.0.GA.jar;%classpath%
set classpath=%libDir%\log4j-1.2.17.jar;%classpath%
set classpath=%libDir%\ognl-3.0.6.jar;%classpath%
set classpath=%libDir%\struts2-core-2.3.16.1.jar;%classpath%
set classpath=%libDir%\xwork-core-2.3.16.1.jar;%classpath%
@echo on
javac -d WEB-INF\classes src\*.java
@echo off
copy src\*.xml WEB-INF\classes
set classpath=%tempClassPath%
@echo on

为了使编译后tomcat自动热加载,在tomcat安装目录下的conf\server.xml中加入了这句(在<host/>节点内):

<Context path="/testStruts" docBase="testStruts" reloadable="true" debug="0"></Context>
上一篇:【转】DHCP的请求过程


下一篇:Java精选笔记_集合概述(Collection接口、Collections工具类、Arrays工具类)