struts2初印象

第一次写这么正式的文章,如果写的不好的地方,请指出。

今天玩了一下struts2,不过貌似是我被他玩了。简要笔记如下:

一、配置struts2(在eclipse Helios版本下)

(1)先创建一个Dynamic Web Project,具体方法如下,File->New->Other->Web->Dynamic Web Project

(2)填好好相关,比如Project name为struts2login,然后不要急着按Finish键,因为新版本的的eclipse是不会自动生成web.xml文件的,所以要乖乖按Next键,接着就会看到一个“Generate web.xml deployment descriptor”,勾上,这样就会在WEB-INF目录下生成web.xml

(3)登录http://struts.apache.org/download.cgi 下载struts2Full Distribution版本。然后解压,将lib目录下的commons-fileupload、common-io、freemarker、

javassist、ognl、struts2-core、xwork-core的jar包复制到刚刚那个Dynamic Web Project 的WebContent/WEB-INF/lib目录下

(4)修改web.xml文件

增加字体比较大的一部分。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>struts2test</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   
13     <filter>
14     <filter-name>struts2</filter-name>
15     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
16     </filter>
17     <filter-mapping>
18     <filter-name>struts2</filter-name>
19     <url-pattern>/*</url-pattern>
20     </filter-mapping>
21 </web-app>

(5)在src目录下编写struts.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
 4     "http://struts.apache.org/dtds/struts-2.1.7.dtd">
 5 <!-- 指定Struts 2配置文件的根元素 -->
 6 <struts>
 7     <!-- 指定全局国际化资源文件 -->
 8     <constant name="struts.custom.i18n.resources" value="mess"/>
 9     <!-- 指定国际化编码所使用的字符集 -->    
10     <constant name="struts.i18n.encoding" value="utf-8"/>
11     <!-- 所有的Action定义都应该放在package下 -->
12     <package name="fong" extends="struts-default">
13         <action name="login" class="LoginAction">
14             <!-- 定义三个逻辑视图和物理资源之间的映射 -->        
15             <result name="input">/login.jsp</result>
16             <result name="error">/error.jsp</result>
17             <result name="success">/welcome.jsp</result>
18         </action>
19     </package>
20 </struts>

(6)编写login.jsp在WebContent目录下,记住不要放到WEB-INF目录下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="login">
    <s:textfield name="username" key="use"/>
    <s:textfield name="password" key="pas"/>
    <s:submit key="login"/>
</s:form>
</body>
</html>
View Code

(7)照6,写error.jsp和welcome.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@taglib prefix="s" uri="/struts-tags" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title><s:text name="errorPage"/></title>
 9 </head>
10 <body>
11 <s:text name="errorInfo"/>
12 </body>
13 </html>
View Code
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3     <%@taglib prefix="s" uri="/struts-tags"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title><s:text name="welcomePage"/></title>
 9 </head>
10 <body>
11 <s:text name="welcomeInfo">
12 <s:param>${sessionScope.user}</s:param>
13 </s:text>
14 </body>
15 </html>
View Code

(8)在src目录下配置mess.properties文件如下

loginPage=登录页面
errorpage=错误页面
welcomePage=欢迎页面
errorInfo=登录输入错误
welcomeInfo=欢迎,{0},登陆成功
use=用户名
pas=密  码
login=提交

(9)使用native2ascii(JDK下的一个工具)生成unicode编码的mess.properties文件,生成文件的名字为mess_zh_CN.properties

loginPage=u767bu5f55u9875u9762
errorpage=u9519u8befu9875u9762
welcomePage=u6b22u8fceu9875u9762
errorInfo=u767bu5f55u8f93u5165u9519u8bef
welcomeInfo=u6b22u8fceuff0c{0}uff0cu767bu9646u6210u529f
use=u7528u6237u540d
pas=u5bc6  u7801
login=u63d0u4ea4

(10)编写LoginAction.java文件,在src目录下

 1 import com.opensymphony.xwork2.ActionContext;
 2 import com.opensymphony.xwork2.ActionSupport;
 3 
 4 public class LoginAction extends ActionSupport
 5 {
 6     /**
 7      * 
 8      */
 9     private static final long serialVersionUID = 1L;
10     private String username;
11     private String password;
12     
13     public String getUsername()
14     {
15         return username;
16     }
17     public void setUsername(String username)
18     {
19         this.username = username;
20     }
21     
22     public String getPassword()
23     {
24         return password;
25     }
26     public void setPassword(String password)
27     {
28         this.password = password;
29     }
30     //定义处理用户请求的execute方法
31     public String execute() throws Exception
32     {
33         //当username为crazyit.org,password为leegang时即登录成功
34         if (getUsername().equals("Fong")
35             && getPassword().equals("haha") )
36         {
37             ActionContext.getContext().getSession()
38                 .put("user" , getUsername());
39         return SUCCESS;
40         }
41         else
42         {
43             return ERROR;
44         }
45     }
46 }
View Code

然后就已经搞定完一个struts2的应用。

被坑的有几处。

(1)UTF-8中文乱码,其实只要在struts.xml中

<constant name="struts.custom.i18n.resources" value="mess"/>

然后就要在src目录下创建mess.properties的文件,然后用native2ascii生成mess_zh_CN.properties

在jsp页面中引用mess.properties中的常量,比如:<s:text name="loginPage"/>,如果有修改mess.properties文件,记得要重新用native2ascii重新生成unicode编码的文件。

(2)如果运行之后出现“

	LoginAction.execute(LoginAction.java:36)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

的错误,可能的一个原因如下,在表单提交的JSP页面中

<s:form action="login">
<s:textfield name="username" key="use"/>
<s:textfield name="password" key="pas"/>
<s:submit key="login"/>
</s:form>

name值是为了给java文件中的Action类传参用的,所以名字必须与java文件中的Action类的函数名字一样,比如

LoginAction中的getUsername()函数和setUsername()函数,如果写成getUserName()和setUserName(),那么这两个函数struts将调用不到。

(U必须为大写,N必须为小写,因为 name="username"中的N为小写,但是U处在两个单词的分隔处)。

原文地址:https://www.cnblogs.com/claruarius/p/struts2impress.html