免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1977 | 回复: 0
打印 上一主题 下一主题

Eclipse JavaEE struts2 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-23 02:14 |只看该作者 |倒序浏览

 
struts.xml中添加:
  1. <package name="struts2" extends="struts-default">
  2.          <!-- action name="login" class="LoginAction" method="execute" -->
  3.          <action name="login" class="LoginAction" method="execute">
  4.                <result name="success">login_success.jsp</result>
  5.                <result name="failure">login_fail.jsp</result>
  6.                <result name="input">index.jsp</result>
  7.          </action>
  8. </package>

web.xml中增加struts2支持:(filter和filter-map)
  1. <filter>
  2.       <filter-name>struts2</filter-name>
  3.       <filter-class>
  4.           org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  5.       </filter-class>
  6. </filter>

  7. <filter-mapping>
  8.       <filter-name>struts2</filter-name>
  9.       <url-pattern>*.action</url-pattern>
  10. </filter-mapping>

  11. <filter-mapping>
  12.     <filter-name>struts2</filter-name>
  13.     <url-pattern>*.jsp</url-pattern>
  14. </filter-mapping>


LoginAction.java中添加函数:
  1. //login.jsp 中直接赋值给name和pwd
  2.     private String name;
  3.     private String pwd;

  4.     //validate() 会先于execute()执行,struts2内置
  5.     public void validate()
  6.     {
  7.         if(getName() == null || "".equals(getName().trim()))
  8.         {
  9.             this.addFieldError("name", "name is required");
  10.         }
  11.         if(getPwd() == null || "".equals(getPwd().trim()))
  12.         {
  13.             this.addFieldError("pwd", "pwd is required");
  14.         }        
  15.     }
  16.     
  17.     public String execute() throws Exception
  18.     {
  19.         LoginCheck check1 = new LoginCheck();

  20.         if(check1.isLogin(getName(), getPwd()))
  21.         {
  22.             ActionContext.getContext().getSession().put("login", "true");
  23.             return "success";
  24.         }else
  25.         {
  26.             return "failure";
  27.         }
  28.     }
添加LoginCheck类:
  1. public class LoginCheck {
  2.     public boolean isLogin(String name, String pwd)
  3.     {
  4.         System.out.println(name);
  5.         System.out.println(pwd);
  6.         if("admin".equals(name) && "123456".equals(pwd)){
  7.             return true;
  8.         }else{
  9.             return false;
  10.         }
  11.     }
  12. }

使用struts2的tag:
<%@taglib prefix="s" uri="/struts-tags" %>


index.jsp的body:
  1. <%-- 直接到达controller --%>
  2.     <s:form action="login">
  3.         <%-- name必须与class中的名字完全一致,struts会自动赋值 --%>
  4.         <s:textfield name="name" label="用户名"></s:textfield>
  5.         <s:password name="pwd" label="密码"></s:password>
  6.         <s:submit value="登录"></s:submit>
  7.     </s:form>
    
常见问题:
(1)警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to
'org.eclipse.jst.jee.server:p1' did not find a matching property
出现SetPropertiesRule警告的原因是因为Tomcat在server.xml的Context节点中不支持source属性
解决方法是在Server的配置界面中选中"Publish module contexts to separate XML files"选项。
(2)
The APR based Apache Tomcat Native library which allows optimal performance in production environments was not
found on the java.library.path:
Google后得知此问题是由于Apache 默认推荐使用ARP提升系统性能,所以在启动Tomcat时会去搜寻这个库,解决方法很简单去
http://www.apache.org/dist/tomcat/tomcat-connectors/native/ 下载最新的库(一般只需要下载tcnative-1.dll 就行了) 放入你JDK安装目录下的bin文件夹即
可...(比如我JDK的Bin文件夹在C:\Program Files\Java\jdk1.6.0_17\bin)
文章地址:http://tomcat.apache.org/tomcat-6.0-doc/apr.html
(3)找不到类:
Since Struts 2.1.3, use StrutsPrepareAndExecuteFilter instead or StrutsPrepareFilter and Strut***ecuteFilter if needing using the ActionContextCleanUp filter in addition to this one
org.apache.struts2.dispatcher.FilterDispatcher --->
---> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
需要手动将struts2-blank.war中的lib拷贝到目标lib中。不能多拷。


 
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP