正确显示引入和使用Struts标签库的JSP页面

来源:互联网 发布:湖北快三遗漏数据查询 编辑:程序博客网 时间:2024/05/15 00:24

正确显示引入和使用Struts标签库的JSP页面

 

如果JSP文件引入struts标签库,并且采用struts标签来显示表单,有两个地方必须做出修改,否则显示JSP页面要报错。

 

1、修改web.xml文件

 

<filter>

   <filter-name>struts2</filter-name>

   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

 

<filter-mapping>

    <filter-name>struts2</filter-name>

    <url-pattern>*.action</url-pattern>

</filter-mapping>

      

<filter-mapping>

    <filter-name>struts2</filter-name>

    <url-pattern>*.jsp</url-pattern>

</filter-mapping> 

 

增加一个过滤器映射(加粗部分),这样就能够过滤JSP文件。

 

如果没有增加该过滤映射,运行引入了struts标签库的JSP页面,会报如下错误。

The Struts dispatcher cannot be found.  This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]

      

2、采用<s: form>标签,action属性值中的".action"必须删除掉。

 

register.jsp:

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>My JSP 'register.jsp' starting page</title>

</head>

 

<body>

       <h2>

              <font color="blue">用户注册</font>

       </h2>

       <s:actionerror cssStyle="color:red"/> <!--显示action级别的错误信息-->

        <hr>

       <s:fielderror cssStyle="color:blue"/><!--显示field级别的错误信息-->

      

       <!-- 如果引入了struts标签库,原来的"register.action"必须去掉".action",否则要抛出异常:

       No configuration found for the specified action: 'register.action' in namespace: ''.

        Form action defaulting to 'action' attribute's literal value.

        -->

       <s:form action="register" method="post">

              <s:textfield name="username" label="username"/>

              <s:password name="password" label="password"/>

              <s:password name="repassword" label="repassword"/>

              <s:textfield name="age" label="age"/>

              <s:textfield name="birthday" label="birthday"/>

              <s:textfield name="graduation" label="graduation"/>

              <s:submit value="submit"/>

       </s:form>

        

        <!--

       <form action="register.action" method="post">

            username: <input type="text" name="username"><br>

            password: <input type="password" name="password"><br>

            repassword: <input type="password" name="repassword"><br>

            age: <input type="text" name="age"><br>

            birthday: <input type="text" name="birthday"><br>

            graduation: <input type="text" name="graduation"><br>

            <input type="submit" value="submit">

        </form>

        -->

</body>

</html>

 

3RegisterAction

 

package cn.hw.struts2;

 

import java.util.Calendar;

import java.util.Date;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class RegisterAction extends ActionSupport {

 

       private String username;

       private String password;

       private String repassword;

       private int age;

       private Date birthday;

       private Date graduation;

 

       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 getRepassword() {

              return repassword;

       }

 

       public void setRepassword(String repassword) {

              this.repassword = repassword;

       }

 

       public int getAge() {

              return age;

       }

 

       public void setAge(int age) {

              this.age = age;

       }

 

       public Date getBirthday() {

              return birthday;

       }

 

       public void setBirthday(Date birthday) {

              this.birthday = birthday;

       }

 

       public Date getGraduation() {

              return graduation;

       }

 

       public void setGraduation(Date graduation) {

              this.graduation = graduation;

       }

 

       @Override

       public String execute() throws Exception {

              return SUCCESS;

       }

 

 

       @Override

       public void validate() {

              if (null == username || username.length() < 4 || username.length() > 6) {

                     this.addActionError("username is invalid");

                     this.addFieldError("username", "username is invalid in field");

              }

 

              if (null == password || password.length() < 4 || password.length() > 6) {

                     this.addActionError("password is invalid");

              } else if (null == repassword || repassword.length() < 4

                            || repassword.length() > 6) {

                     this.addActionError("repassword is invalid");

              } else if (!password.equals(repassword)) {

                     this.addActionError("two passwords are not the same");

              }

 

              if (age < 10 || age > 50) {

                     this.addActionError("age is invalid");

              }

 

              if (null == birthday) {

                     this.addActionError("birthday is invalid");

              }

 

              if (null == graduation) {

                     this.addActionError("graduation is invalid");

              }

 

              if (null != birthday && null != graduation) {

                     Calendar c1=Calendar.getInstance();

                     c1.setTime(birthday);

                     Calendar c2=Calendar.getInstance();

                     c2.setTime(graduation);

                     if(c1.after(c2)){

                            this.addActionError("birthday should be before graduation");

                     }                   

              }

       }

}

 

4struts.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="struts2" extends="struts-default">             

              <action name="register" class="cn.hw.struts2.RegisterAction">           

                     <result name="success">/registerResult.jsp</result>

                     <result name="input">/register.jsp</result>

              </action>          

       </package>

</struts>

 

5regesterResult.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>    

    <title>My JSP 'registerResult.jsp' starting page</title>

  </head>

  

  <body>

         username: <s:property value="username"/><br>

         password: <s:property value="password"/><br>

         age: <s:property value="age"/><br>

         birthday: <s:property value="birthday"/><br>

         graduate: <s:property value="graduation"/>

  </body>

</html>

 

6、启动服务器,运行register.jsp页面

 

 正确显示引入和使用Struts标签库的JSP页面

输入不合法的数据:

正确显示引入和使用Struts标签库的JSP页面

 
单击【submit】按钮,进行服务器端输入校验。

  正确显示引入和使用Struts标签库的JSP页面

输入合法的数据:

 正确显示引入和使用Struts标签库的JSP页面

 

 通过输入校验,显示registerResult.jsp页面:

 正确显示引入和使用Struts标签库的JSP页面

原创粉丝点击