HelloStruts2

    很久没进行web开发,感觉快要忘光了,重拾起快遗忘的记忆,从struts2基础学起。
    第一步,当然是从官网下载最新的struts2,地址http://struts.apache.org/,我习惯下载-all.zip,解压,准备开始第一个struts2。

    第二步,打开MyEclipse,我用的是MyEclipse10,新建一个Dynamic web project工程,命名为“HelloStruts2”。

    第三步,将解压后lib目录下的commons-logging-1.1.1.jar、freemarker-2.3.19.jar、ognl-3.0.5.jar、struts2-core-2.3.4.1.jar、xwork-core-2.3.4.1.jar,这个版本的struts2同时需要commons-fileupload-1.2.2.jar、commons-io-2.0.1.jar、commons-lang3-3.1.jar。这样子貌似可以了,但当运行时死活不行,网上查了后说仍需要一个不存在struts.zip里面的javassist-3.7.ga.jar.......这样在MyEclipse直接run on server运行通过,但部署到tomcat,通过tomcat运行的时候报了class cast异常,最终发现在tomcat的context.xml文件里面加入<Loader delegate="true" />元素后总算可以正常运行了。

    第四步,修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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">
	<display-name>HelloStruts2</display-name>

	<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>
</web-app>


    第五步,在HelloStruts2的WebContent文件夹下新建一个login.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form action="login.action" method="post">
    	username:<input type="text" name="username"><br/>
    	password:<input type="password" name="password"><br/>
    	<input type="submit" value="submit">
    </form>
  </body>
</html>


    第六步,新建一个action,不用像struts1一样强制继承action,更容易维护和写单元测试,也不会与servletAPI联系过紧。
package cn.com.hellostruts2.action;

public class LoginAction {
	private String username;

	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() throws Exception {
		return "success";
	}
}


    第七步,在src目录下新建一个struts.xml文件,MyEclipse可以替我们生成xml with struts,但我更偏向从struts2.zip解压包中的app文件夹中的war中解压提取一个struts.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="struts2" extends="struts-default">
		<action name="login" class="cn.com.hellostruts2.action.LoginAction">
			 <result name="success">/result.jsp</result>  
		</action>
	</package>
</struts>


    第八步,在WebContent文件夹下新建一个result.jsp文件。
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  	username:${requestScope.username } <br/>
  	password:${requestScope.password } 
  </body>
</html>


    以上是代码的编写工作,测试的话可以直接通过MyEclispe集成tomcat,并用run on server来运行,或者右键点击HelloStruts2这个项目的名称,在菜单中选择myeclipse,再选择add and remove project,配置发布的地址就可以了。
   最后,运行tomcat,打开浏览器,在浏览器中输入 http://localhost:8080/struts2/login.jsp 就可以了。

   重拾struts2,发现很多东西都忘了,但还是觉得入门其实不难,用了框架,简化了很多的操作,以后继续认真学习web开发。

猜你喜欢

转载自aquarion.iteye.com/blog/1721903
2
>&2
α2
2-2