第2讲 --Struts2的类型转换

 

 

本节目的:

 

1.改写login.jsp 使用struts2标签的形式,这样才可以完成 2

 

2.让Action集成自ActionSupport ,重写其中的validate()方法 完成校验

 

3.判断用户名为 hello ,密码为 world 才到成功页面

 

 

步骤:

  1.先把标签导入进来,标签的tld 文件在struts-2.0.14-all\struts-2.0.14\lib\struts2-core-2.0.14\META-INF\struts-tags.tld ,放在WEB-INF下

 

2.使用标签改写页面

 

<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="oscache" prefix="cache"%>
<%@ taglib uri="/WEB-INF/struts-tags.tld" prefix="s"%>
<%@page import="java.util.Date"%>
<HTML>
<body>

没有缓存的日期: <%= new Date() %><p>
<!--自动刷新-->
<cache:cache time="30">
每30秒刷新缓存一次的日期: <%= new Date() %> 
</cache:cache>
<!--手动刷新-->
<cache:cache key="testcache">
手动刷新缓存的日期: <%= new Date() %> <p>
</cache:cache>
<a href="cache2.jsp">手动刷新</a>


	<s:form action="login.action" method="post">
	<s:textfield name="username" label="用户名"></s:textfield>
	<s:password name="password" label="密码"></s:password>

	<s:submit  value="提交"></s:submit>
	</s:form>
</body>

</HTML>

 

3.写action

package cn.com.xinli.test.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
 private String username;
 private String password;
 
 /*
  * 客户端和Action的匹配是按方法的名字
  * 而不是属性名字
  * 比如客户端发过来的是password则会找setPassword
  * 而不是找属性password
  * 
  * */
 public String getUsername()
 {
  return username;
 }
 public void setUsername(String username)
 {
  this.username = username;
 }
 public String getPassword()
 {
  return password;
 }
 public void setPassword(String password)
 {
  this.password = password;
 }
 public String execute() throws Exception
 {
  /*好的习惯常量放在前面,则不会抛出空指针异常*/
  if(!"hello".equals(this.getUsername().trim()) || !"world".equals(this.getPassword().trim()))
  {
   /*如果出现错误,错误信息加在username字段的上面*/
   this.addFieldError("username", "用户名或密码错误!");
   return "failer";
  }
  /*小写,默认会找result标签配置的页面*/
  return "success";
 }
 @Override
 public void validate()
 {
  if(null==this.getUsername() || "".equals(this.getUsername().trim()))
  {
   /*增加一个错误,这里可以使用国际化*/
   /*第一参数为表单字段的名字,第2个参数为表单错误信息*/
   this.addFieldError("username", "用户名不能为空!");
  }
  if(null==this.getPassword() || "".equals(this.getPassword().trim()))
  {
   /*增加一个错误,这里可以使用国际化*/
   /*第一参数为表单字段的名字,第2个参数为表单错误信息*/
   this.addFieldError("password", "密码不能为空不能为空!");
  }
 }
 
}

 

4.写配置文件

<?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="struts2" extends="struts-default">
		<action name="login" class="cn.com.xinli.test.action.LoginAction">
			<result>/result.jsp</result>
			<!-- input 为规定的写法,表明表单验证失败的时候转向的页面 -->
			<result name="input">/login.jsp</result>
			<result name="failer">/login.jsp</result>
		</action>
	</package>
	
   

</struts>

 

5.打开tomcat,运行http://localhost:9090/struts2/login.jsp

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值