首页 > 代码库 > Struts2——Action(三)
Struts2——Action(三)
十、 Struts2的中文处理
<constant name=”struts .i18n.encoding” value=http://www.mamicode.com/”GBK”>
Struts2的帮助文档的查看:Struts\docs\index.html
(1)编写index.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>
使用action属性接收参数,测试中文问题
<form action="user/user" method="post">
姓名:<input type="text" name="name"></input>
<input type="submit" value="submit"/>
</form>
</body>
</html>
(2) 编写struts.xml,映射action
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--
<constant name="struts.enable.DynamicMethodInvocation" value=http://www.mamicode.com/"false" />
<constant name="struts.devMode" value=http://www.mamicode.com/"false" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
-->
<constant name="struts.i18n.encoding" value="GBK" />
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="com.zgy.encoding.UserAction" method="add">
<result>
/user_add_success.jsp
</result>
</action>
</package>
<!-- Add packages here -->
</struts>
(3)编写UserAction.java
package com.zgy.encoding;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String add(){
System.out.println("name="+name);
return SUCCESS;
}
}
(4)编写user_add_success.jsp
<%@ 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 ‘user_add_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>
user add success <br>
</body>
</html>
(5) 浏览器端访问
http://localhost:8080/CharacterEncoding/,输入中文:张三
查看后台输出
十一、简单的数据校验
使用addFieldError()方法和s:fieldError标签处理简单的数据校验
JSP中使用struts标签:
<%@taglib uri=”/struts-tags” prefix=”s”%>
范例:
(1)创建index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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="user/user" method="post">
姓名:<input type="text" name="name"></input>
<input type="submit" value="submit"/>
</form>
</body>
</html>
(2)配置struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--
<constant name="struts.enable.DynamicMethodInvocation" value=http://www.mamicode.com/"false" />
<constant name="struts.devMode" value=http://www.mamicode.com/"false" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
-->
<constant name="struts.i18n.encoding" value="GBK" />
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="com.zgy.validate.UserAction" method="add">
<result name="success">
/user_add_success.jsp
</result>
<result name="error">
/user_add_error.jsp
</result>
</action>
</package>
<!-- Add packages here -->
</struts>
(3)编写UserAction.java
package com.zgy.validate;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
public String add(){
if(name == null || name.equals("admin")){
this.addFieldError("name", "name is error");
return ERROR;
}
else{
return SUCCESS;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(4)编写user_add_error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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 ‘user_add_error.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>
user add error
<s:fielderror fieldName="name" theme="simple"></s:fielderror><br />
<s:property value="error.name[0]"/>
<s:debug></s:debug>
</body>
</html>
Struts2——Action(三)