struts 2 学习1

struts2-core.jar    Struts2的核心;
xwork.jar           前面提到的xwork;
ognl.jar            用于页面输出的表达式语言;
freemarker.jar      用户界面标签模版;
commons-logging.jar Apache的公用包,用于输出日志。

struts2- 2.1.6  版本需要添加(加强了上传文件功能)

commons-fileupload-1.2.1.jar

struts2- 2.1.6  版本将datetimepicker一些移入 dojo  包内

struts2-dojo-plugin-2.1.6.jar

<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>

<sx:head parseContent="true"/>
<sx:datetimepicker ></sx:datetimepicker>

 

 

 

 <%@ taglib prefix="c" uri="/struts-tags" %>
引用标签,在file:///F:/5.51workspace/strutsTest/WebRoot/WEB-INF/lib/struts2-core-2.0.11.jar包中struts-2.0.dtd

<c:form action="login.action" method="post">
      <c:textfield name="userName" id="userName" label="userName"></c:textfield>
      <c:textfield name="userPwd" id="userPwd" label="userPwd"></c:textfield>
      <c:submit value="commit"></c:submit>
  </c:form>

 

---web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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>

 

 

 

---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="strust2" extends="struts-default">
   
    <action name="login" class="com.struts.action.LoginAction" method = "执行指定方法名,默认方法不执行">
        <result name="success">/result.jsp</result> ---可以不写,默认成功页
        <result name="input">/index.jsp</result>    ---默认错误页
        <result name="failer">/index.jsp</result>    ---自定义返回字符串
    </action>
     
    </package>
</struts>

 

 

通配符表示方法

<struts>
    <package name="strust2" extends="struts-default">
    <action name="*">   --传入什么   x.action
        <result>/{1}.jsp</result> -- 调用  x.jsp 页面
    </action>
    </package>
</struts>

 

--java action 类
package com.struts.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
    private String userName;
    private String userPwd;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserPwd() {
        return userPwd;
    }
    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }
    public String execute(){
        if("abc".equals(userName.trim())){
            return "success";
        }else{
            addFieldError("userName","userName is abc");
            return "failer";
        }
       
    }
    @Override
    public void validate() {
        // TODO Auto-generated method stub
        if(null==userName||"".equals(userName.trim())){
            this.addFieldError("userName", "userName is null");
        }
        if(null==userPwd||"".equals(userPwd.trim())){
            this.addFieldError("userPwd", "userPwd is null");
        }
    }
   
}


action="login.action"的login对应struts.xml里的name :<action name="login" class="com.struts.action.LoginAction">
login 把 action 类 关联起来 com.struts.action.LoginAction


继承 ActionSupport 类 有验证的方法 public void validate() ,在里面重写验证条件,用 ActionSupport 类的 this.addFieldError("userPwd", "userPwd is null");方法加入错误信息,返回页面 userPwd为<c:textfield name="userPwd"
默认的返回错误name为 input
默认的成功字符串为 "success"

 

 

 

 

 

action 警告解决方法

 

警告: No configuration found for the specified action: 'ShowMessage' in namespace: ''. Form action defaulting to 'action' attribute's literal value.

 

 

<s:form action="user" namespace="/strust2">

对应

<package name="strust2" extends="struts-default">

 

找寻 action 命名空间:

 

因为开始使用的struts2标签(form)并未指定namespace属性。所以struts2会默认从根命名空间"/" 搜索action '/hello/ShowMessage.action',如搜索不到则进入默认命名空间''搜索action串,在默认命名空间中是肯定找不到自己定义的action的,所以,struts2抛出一个警告信息。

现在我们指定了namespace为/hello,则struts2会直接在/hello命名空间寻找。可以想象,如果在这么命名空间里找不到请求的action,也会抛出一条类似的信息。

注意,<s:form name="ShowMessage" method="post" action="ShowMessage" namespace="/hello" >
不能写成<s:form name="ShowMessage" method="post" action="ShowMessage.action" namespace="/hello" >

这样仍然有对应的警告信息,并且提交后出现无法访问的结果。因为没有ShowMessage.action这样一个action,这个.action不能由我们手工添加,Struts2会自动为你完成这个工作,并且手工添加是不行的,就不必多此一举了。但是在其他的场合,比如使用超级链结,则可以加上这个.action

 

 

 

 

 

 

 

 

 

 

第一,在web.xml中添加配制

Xml代码
  1. <filter>  
  2.     <!-- 定义核心 Filter 名字 -->  
  3.     <filter-name>Struts 2</filter-name>  
  4.     <!-- 定义核心 Filter 的实现类 -->  
  5.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  6. </filter>  
  7.   
  8. <!-- FilterDispatcher 用来初始化 Struts 2 并且处理所有请求 -->  
  9. <filter-mapping>  
  10.     <filter-name>Struts 2</filter-name>  
  11.     <url-pattern>/*</url-pattern>  
  12. </filter-mapping>  


添加到web-app中

第二,添加struts2配制

Xml代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.         "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <!-- 指定全局国际化资源文件base名 -->  
  8.     <constant name="struts.custom.i18n.resources" value="messageResource"></constant>  
  9.     <!-- 指定国际化编码所使用的字符集 -->  
  10.     <constant name="struts.i18n.encoding" value="utf-8"></constant>  
  11.     <!-- 指定需要Struts2处理的请求后缀,默认值是action,也可配制多个,用","分开 -->  
  12.     <constant name="struts.action.extension" value="do"></constant>  
  13.     <!-- 指定是否允许在Struts2中使用表达式语法,默认是true -->  
  14.     <constant name="struts.tag.altSyntax" value="true"></constant>  
  15.     <!-- 指定是否使用开发模式,默认为false,设置true时,可在应用出错时显示更多更友好的出错信息,通常开发设置true,产品发布后设置false -->  
  16.     <constant name="struts.devMode" value="true"></constant>  
  17.     <!-- 指定是否每次HTTP请求时都重新加载资源文件,默认为false,设置true时,通常开发设置true,产品发布后设置false,但不知道为什么,设置一直都不成功 -->  
  18.     <constant name="struts.i18n.reload" value="true"></constant>   
  19.     <!-- 指定struts.xml文件改变后是否自动重新加载该文件,默认为false,设置true时,通常开发设置true,产品发布后设置false -->  
  20.     <constant name="struts.configuration.xml.reload" value="true"></constant>  
  21.   
  22.   
  23.   
  24.     <!--  
  25.        定义包,每个Action都必须定义在包内,且包与包可以进行继承,继承后父包中所有的Action   
  26.        name 包名,唯一标识附  
  27.        extends 继承,需要继承包的name属性,如无自己手动定义的包,哪么必须要继承struts-default包  
  28.        namespace 命名空间,访问时的命名空间名,以“/”开头  
  29.       -->  
  30. <package name="semcolon" extends="struts-default" namespace="/login">  
  31.           
  32.         <!-- 定义Action  
  33.            name Action名,同时也是Action访问路径的前缀  
  34.            class 实现Action的java类  
  35.           -->  
  36.         <action name="loginAction" class="semcolon.login.action.LoginAction">  
  37.             <!-- Action返回路径,如不设置name属性,将默认为success,路径为全路径 -->  
  38.             <result name="login">/WEB-INF/jsp/login/login.jsp</result>  
  39.             <result>/WEB-INF/jsp/comm/main.jsp</result>  
  40.         </action>  
  41.           
  42.     </package>  
  43.       
  44. </struts>  




第三,实现Action类

Java代码
  1. package semcolon.login.action;  
  2.   
  3. import semcolon.bean.UserBean;  
  4. import semcolon.comm.action.BaseAction;  
  5. import semcolon.comm.constants.ExceptionInfo;  
  6.   
  7. public class LoginAction extends ActionSupport{  
  8.     // 用户信息Bean  
  9.     private UserBean user = null;  
  10.   
  11.     public UserBean getUser() {  
  12.         return user;  
  13.     }  
  14.   
  15.     public void setUser(UserBean user) {  
  16.         this.user = user;  
  17.     }  
  18.   
  19.     // 定义处理用户请求的execuit方法  
  20.     public String execute() throws Exception {  
  21.       
  22.         if (getUser().getUsername().equals("semcolon")  
  23.                 && getUser().getPassword().equals("123")) {  
  24.               
  25.             return SUCCESS;  
  26.         }  
  27.           
  28.         return LOGIN;  
  29.     }  
  30.   
  31. }  
package semcolon.login.action;

import semcolon.bean.UserBean;
import semcolon.comm.action.BaseAction;
import semcolon.comm.constants.ExceptionInfo;

public class LoginAction extends ActionSupport{
	// 用户信息Bean
	private UserBean user = null;

	public UserBean getUser() {
		return user;
	}

	public void setUser(UserBean user) {
		this.user = user;
	}

	// 定义处理用户请求的execuit方法
	public String execute() throws Exception {
	
		if (getUser().getUsername().equals("semcolon")
				&& getUser().getPassword().equals("123")) {
			
			return SUCCESS;
		}
		
		return LOGIN;
	}

}


继承ActionSupport类,且实现execute方法就可以了

最后为两个JSP页面
login.jsp

Html代码
  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. <title>login</title>  
  8. </head>  
  9. <body>  
  10. <s:form action="loginAction.do" method="post" >  
  11.     <s:textfield name="user.username" label="姓名"/>  <br>  
  12.     <s:textfield name="user.password" label="密码"/>  
  13.     <s:submit/>  
  14. </s:form>  
  15. </body>  
  16. </html>  
<%@ 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>
<title>login</title>
</head>
<body>
<s:form action="loginAction.do" method="post" >
	<s:textfield name="user.username" label="姓名"/>  <br>
	<s:textfield name="user.password" label="密码"/>
	<s:submit/>
</s:form>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值