struts2初学之----初出茅庐

   我是一名java程序员,从去年12月来到我现在所在的公司,到现在已经有5个月了,按理说有一定的经验了,可是事实情况是怎么样呢?
   12月份刚入职,就被安排写文档,实际上是补文档,上一个项目的文档不全,我们好几个人大概写了一个月的文档,接下来就被调到某银行了。我们公司采用的是自己开发的eclipse插件,属于快速开发的那种,所以代码写的会很少,一直到现在5月份,整整5个月,我写的java代码加起来也不到300行,想想真是很悲哀,我决定摆脱这种情况,其实从内心来说,对于一个刚入门的初级程序员,我还是很想多写一些代码的,这样最起码也能得到很好的锻炼吧。所以,我开始从SSH框架开始学起,主要为了摆脱目前的状况。
   我之前是学过struts的,1.x,2.x我都学过的,但是那也仅仅局限于helloworld级别的自娱自乐的例子程序。现在我打算花三个月时间,当然由于我目前在职,是业余时间了,把SSH轻量级框架好好学习一遍,为下一份工作多少积累点资本。当然,我并不是说SSH有多么的重要,但是最起码能给我加一点筹码。
   众所周知的是,struts的第一个版本诞生于2001年6月。当时很多系统都是采用模式一:JSP+SERVLET+JAVABEAN开发出来的,在JSP写数据库访问代码,其实后来看来是很麻烦的做法。在那种维护噩梦的年代,apache觉得有必要改变这种现状,所以开始了struts的开发。
   我目前实际上并不是很忙,但是手头工作还是第一位的;其次就是为了下一份工作准备,所以,我就直接从struts2开始学习。
   struts2是一个表示层框架,就我目前的了解,它通常可以做为系统的请求分派器,任何请求都可以由它拦截到,当然可以做一些简单的验证,struts2里,我们通常要在web.xml中配置一个filter,该过滤器的完整名字是:org.apache.struts2.dispatcher.FilterDispatcher;
该类是一个过滤器,
我做了一个简单的例子,内容如下
web.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>ssh.demo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

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>
<constant name="struts.action.extension" value="action,do"></constant>
    <package name="default" extends="struts-default">
        <action name="test" class="example.TestAction">
            <result name="success">/example/success.jsp</result>
            <result name="input">/example/input.jsp</result>
        </action>
</package>
</struts>

TestAction代码:
package example;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {
/**
* @author renmeng
*/
private static final long serialVersionUID = 1L;
private String str1;
private String str2;
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
public String getStr2() {
return str2;
}
public void setStr2(String str2) {
this.str2 = str2;
}

public String execute() throws Exception {
if(!str1.equals("")&&!str2.equals("")) {
ActionContext.getContext().getSession().put("str1", str1);
ActionContext.getContext().getSession().put("str2", str2);
return Action.SUCCESS;
}else {
return Action.INPUT;
}
}
}

input.jsp和success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="test.action" method="post">
<input type="text" name="str1"></input><br/>
<input type="text" name="str2"></input><br/>
<input type="submit"></input>
</form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="300" border="1">
<tr>
<td>str1=<%=(String)session.getAttribute("str1") %></td>
</tr>
<tr>
<td>str2=<%=(String)session.getAttribute("str2") %></td>
</tr>
<tr>
<td><a href="input.jsp">[BACK]</a></td>
</tr>
</table>
</body>
</html>

认真看的话,应该没有问题,另外值得一提的是struts2.2版本需要加入javassist-3.12.0.GA.jar包,而奇怪的是struts的lib里是没有这个包的,大家可以去hibernate里去找。
   今天先写到这,该睡觉了,明天还要上班。

猜你喜欢

转载自renmeng639.iteye.com/blog/1050900