STRUTS2-based Global type converter

Source: Internet
Author: User



Loginaction.java
public class Loginaction implements Action { //Use type converter to convert string request parameters directly into a user instanceprivate user user;     Private User customer; Private Date birth; //Encapsulate Tip attributes for action processing results     private String tip;     public void setuser (user user)  {         this.user = user;    &NBSP}     public void setcustomer (User customer)  {         this.customer = customer;    &NBSP}     public void settip (String tip)  {         this.tip = tip;    &NBSP}     public void setbirth (Date d)  {         this.birth = d;    &NBSP}     public user getuser ()  {         return  (This.user);     }     public user getcustomer()  {        return  (this.customer);     }     public date getbirth ()  {         return this.birth;    &NBSP}     public string gettip ()  {         return  (This.tip);    &NBSP}     public string execute ()  throws Exception  {        if  (GetUser (). GetName (). Equals ("admin")                  &&  GetUser (). Getpass (). Equals ("admin"))  {             settip ("Conversion successful");             return SUCCESS;          } else {            settip ("Conversion failed" );             return ERROR;         }    &nbsp}} Userconverter.java
public class Userconverter extends Strutstypeconverter{Public Object convertfromstring(Map context, string[] values, Class toclass)         {User user = new user ();         string[] uservalues = Values[0].split (",");         User.setname (Uservalues[0]);         User.setpass (Uservalues[1]);     return user; @Override Public String converttostring(Map context, Object O)         {User user = (user) O;     Return "<" + user.getname () + "," + user.getpass () + ">"; } } User.java
public class User {private String name;     Private String Pass;     public void SetName (String name) {this.name = name;     public void SetPass (String pass) {This.pass = pass;     Public String GetName () {return (this.name);     Public String Getpass () {return (This.pass); } } Struts.xml
<struts> <package name= "Jcuckoo" extends= "Struts-default" > <action name= "Login" class= "Jcuckoo" .     Loginaction "> <result name=" Success ">/welcome.jsp</result> </action> </package> </struts> xwork-conversion.properties
Jcuckoojcuckoo.user=jcuckoo.userconverter Web.xml
<!--define STRUTS2 's filterdispathcer filter--> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> & lt;! --Filterdispatcher is used to initialize STRUTS2 and process all Web requests. --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*< /url-pattern> </filter-mapping> input.jsp
<%@ page language= "java"  contenttype= "TEXT/HTML;&NBSP;CHARSET=GBK"%> welcome.jsp
<%@ page language= "java"  contenttype= "TEXT/HTML;&NBSP;CHARSET=GBK"%> <% @taglib   Prefix= "S"  uri= "/struts-tags"%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.