首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Struts2接受数据的三种模式

2013-10-23 
Struts2接受数据的三种方式怎么说呢,今天是接触Struts2的第二天了。关于Struts2来说,也不能说难。只能说复杂

Struts2接受数据的三种方式

  怎么说呢,今天是接触Struts2的第二天了。关于Struts2来说,也不能说难。只能说复杂。也难怪,WEB这个方向能庞大,里面的东西能不复杂吗。但话又说回来,复杂也有其复杂的好处。比如,

jsp+javaBean简单,但那是往小了说,如果项目足够庞大,Struts2 的好处就显现出来了。产品不是做出来就算牛逼了。关键还是在于后期的维护,如果项目的扩展性很好,耦合度很小,那么后期的维护 也便方便些。所以不管怎么讲,再复杂也得研究,也得学。只要不是笨到只会左脚迈步的人,什么东西都是能学会的。废话也不多讲了。

关于Struts2的学习,只要是对大家有好处的东西,我都会及时写博客的,望大家一块学习,一块进步。

a、  使用领域对象接受用户输入:input接受参数的名字要命成领域对象.名称(user.username, user.password)。原理:通过Struts2框架的数据绑定机制,传递user.username请求参数等同于调用:action.getUser().setUsername(…)。

实例:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%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>    <form action="login.action" method="post">    <table>    <tr>    <td>    Username:<input type="text" name="user.username" />    </td>    </tr>    <tr>    <td>    Password:<input type="password" name="user.password" />    </td>    </tr>    <tr>    <td>    <input type="submit" value="Save">    </td>    </tr>    </table>    </form>  </body></html>
User.java:public class User {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;}private String username;private String password;}LoginAction.java:import com.opensymphony.xwork2.Action;public class LoginAction implements Action{private User user;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public String execute() throws Exception {if("cuijun".equals(user.getUsername()) && "123".equals(user.getPassword())){return SUCCESS;}return ERROR;}}


 

Success.jsp:<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%@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>    <h3><s:property value="user.username" /></h3>  </body></html>error.jsp:<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%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>    用户名和密码错误,请重新登录<a href="login.jsp">  </body></html>struts.xml:<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts>   <package  name="default" extends="struts-default"> <action name="login" class="LoginAction"> <result>/success.jsp</result> <result name="error">/error.jsp</result> </action> </package></struts>


 

a、直接使用action的属性接受用户输入。也就是把<input>里的user.username和user.password转换成username和password。而在LoginAction.java中直接定义username和password属性。不再使用User。同时也要生成username和password的setter和getter方法。

b、使用ModelDriven action。只需把修改LoginAction.java,如下:

import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ModelDriven;public class LoginAction implements Action, ModelDriven{private User user = new User();public String execute() throws Exception {if("cuijun".equals(user.getUsername()) && "123".equals(user.getPassword())){return SUCCESS;}return ERROR;}public Object getModel() {// TODO Auto-generated method stubreturn user;}}


 

LoginAction实现了ModelDriven接口,表单字段就不需要再使用“user.”前缀了。


 

热点排行