Ananda
Ananda

Reputation: 1572

How to use css styles in struts2 tags?

I am using metro CSS theme in my struts2 application. I was using html tags with CSS classes which was working fine. But while creating validation logic I found that It does validate the form but doesn't show any error.

<form action="register.action" method ="post" validate="true">
 <div class="input-control text span3">
    <input type="text" name="username" placeholder="Enter User Name" value="" />
    <span class="helper"></span>
 </s:div> 
</form>

then I switched to struts2 html tags and it started showing errors but now I have no style attached. I used cssClass attribute of struts2 html tags to give style to form and div tags which worked well but after changing <input type="text"> tag to <s:textfield> all the style attached to input box is gone.

Please suggest me how to use struts tags preserving CSS styles.

Upvotes: 2

Views: 27861

Answers (2)

Aleksandr M
Aleksandr M

Reputation: 24396

If you already have all JSP-s with your own styles and elements use Struts2 simple theme. This theme will not create additional elements nor show validation errors. But you can use fielderror tag for showing validation errors http://struts.apache.org/2.x/docs/fielderror.html. So you form will look something like that:

<form action="register.action" method ="post" validate="true">
  <div class="input-control text span3">
    <s:fielderror fieldName="username"/>
    <s:textfield name="username" placeholder="Enter User Name"/>
    <span class="helper"></span>
  </s:div>
</form>

You can define default theme for the whole application inside your struts.xml file by using struts.ui.theme constant.

<constant name="struts.ui.theme" value="simple" />

Upvotes: 4

Viral Patel
Viral Patel

Reputation: 8601

You can use cssClass property of <s:textfield> tag and add CSS styles to styleup your form.

<s:textfield cssClass="mytext" ... />

//css
.mytext {
}

One thing worth noting here is that Struts2 tag uses xhtml theme by default. Thus it generates lot of html code along with form. You may want to change the default theme to css_xhtml to apply your own styles instead of using Struts2 default.

Add following in struts.properties file. struts.ui.theme = css_xhtml

Reference: Override Default Theme in Struts 2

Upvotes: 8

Related Questions