struts类型转换的一种简便方法
来源:互联网 发布:淘宝怎么看行业数据库 编辑:程序博客网 时间:2024/04/30 06:52
开发web网站,必然要涉及到类型转换,用struts2框架来开发的话,其中有很多相关方法,核心的一个思想就是将页面中需要提交的内容封装到一个类,然后由某个action调用。根据这个思想,将其中的一个方法的代码粘贴出来:
1 先定义一个javabean 即一个用户类,就两个字段,username和password。
private String username;
private String password;
public User(){}
public User(String username, String password) {
this.username = username;
this.password = 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;
}
}
2 创建一个action,处理页面表单元素
public class LoginAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
System.out.println(user.getUsername());
System.out.println(user.getPassword());
return "success";
}
}
3创建一个两个jsp,分别是index.jsp和success.jsp
index.jsp如下
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form action="Login">
<!-- 下面两行代码会自动调用user的set 方法 -->
<s:textfield name="user.username" label="名字"></s:textfield>
<s:password name="user.password" label="密码"></s:password>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
备注:其中user.username的user字段要和action中的user名字相同,还有这里用到struts标签,这样就会自动调用username和password的set方法,进而封装。
success.jsp 显示效果
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:property value="user"/>
</body>
</html>
4.最后一步配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="test" extends="struts-default">
<action name="Login" class="com.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
- struts类型转换的一种简便方法
- 一种简便实效的配置文件读写方法
- 一种简便实效的配置文件读写方法
- 一种简便实效的配置文件读写方法
- 一种简便获取iPhone IMEI 的方法
- 一种更新yum源的简便方法
- 二进制转换为十进制的简便方法
- 一种非常简便的实现Android多线程池的方法
- Flash文字描边的一种简便方法
- 一种通过注册表简便修改IE代理服务器的方法
- 通过ODBC远程访问MySQL的一种简便方法
- VC下显示JPG,GIF图象的一种简便方法
- 编程创建XML文档的一种简便方法
- 一种保持VC对话框布局的简便方法
- 一种保持VC对话框布局的简便方法
- 一种保持VC对话框布局的简便方法
- 一种快捷简便的WIN CE界面定制方法
- 子窗口控制父窗口一种简便的方法
- UITableView详解
- 安装TOMCAT
- 计算机0和1
- Java面试题目系列二
- W5100E01-AVR是什么?怎么用?(五)(用户手册V1.0版)
- struts类型转换的一种简便方法
- memset用法详解
- computer vision(计算机视觉)方面的期刊会议,学术必备
- Cross Browser CSS Opacity and the JavaScript Fade / Fading Effect(跨浏览器的CSS透明和JavaScript淡入淡出效果)
- prim zoj 1406 Jungle Roads
- 继续聊WPF——Expander控件
- Android仿iPhone的时间轮的工具Demo
- 【经典】FPGA三国志
- C# 反射机制(转)