struts.xml中添加:
- <package name="struts2" extends="struts-default">
- <!-- action name="login" class="LoginAction" method="execute" -->
- <action name="login" class="LoginAction" method="execute">
- <result name="success">login_success.jsp</result>
- <result name="failure">login_fail.jsp</result>
- <result name="input">index.jsp</result>
- </action>
- </package>
web.xml中增加struts2支持:(filter和filter-map)
- <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>*.action</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>*.jsp</url-pattern>
- </filter-mapping>
LoginAction.java中添加函数:
- //login.jsp 中直接赋值给name和pwd
- private String name;
- private String pwd;
- //validate() 会先于execute()执行,struts2内置
- public void validate()
- {
- if(getName() == null || "".equals(getName().trim()))
- {
- this.addFieldError("name", "name is required");
- }
- if(getPwd() == null || "".equals(getPwd().trim()))
- {
- this.addFieldError("pwd", "pwd is required");
- }
- }
-
- public String execute() throws Exception
- {
- LoginCheck check1 = new LoginCheck();
- if(check1.isLogin(getName(), getPwd()))
- {
- ActionContext.getContext().getSession().put("login", "true");
- return "success";
- }else
- {
- return "failure";
- }
- }
添加LoginCheck类:
- public class LoginCheck {
- public boolean isLogin(String name, String pwd)
- {
- System.out.println(name);
- System.out.println(pwd);
- if("admin".equals(name) && "123456".equals(pwd)){
- return true;
- }else{
- return false;
- }
- }
- }
使用struts2的tag:
<%@taglib prefix="s" uri="/struts-tags" %>
index.jsp的body:
- <%-- 直接到达controller --%>
- <s:form action="login">
- <%-- name必须与class中的名字完全一致,struts会自动赋值 --%>
- <s:textfield name="name" label="用户名"></s:textfield>
- <s:password name="pwd" label="密码"></s:password>
- <s:submit value="登录"></s:submit>
- </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时会去搜寻这个库,解决方法很简单去
可...(比如我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中。不能多拷。
|