表单提交有中文尽量使用post
<constant name="struts.i18n.encoding" value="GBK"/> 这是一个bug
<filter-class>org.apache.struts2.dispatcher.FileterDispatcher</filter-class> web.xml 说可以解决不过我没有试出来就没做了,用spring 解决乱码安全
- package com.demo.action;
- import com.opensymphony.xwork2.ActionSupport;
- public class UserAction extends ActionSupport {
- private String name;
- public String add() {
- System.out.println("name = " + name);
- return SUCCESS;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- body>
- 使用action 属性接收参数,测试中文问题
- <form action="user/user!add" method="post">
- 姓名:<input type="text" name="name"></input>
- <input type="submit" value="submit"/>
- </form>
- </body>
数据校验
- package com.demo.action;
- import com.opensymphony.xwork2.ActionSupport;
- public class UserAction extends ActionSupport {
- private String name;
- public String add () {
- if(name == null || !name.equals("admin")) {
- this.addFieldError("name", "name is error");
- // this.addFieldError("name", "name is long");
- return ERROR;
- }
- return SUCCESS;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- <body>
- 使用addFiledError 方法和s:filedError 标签简单处理数据校验
- <a href="user/user!add?name=a">添加用户</a>
- </body>
- <body>
- User Add Error!
- <s:fielderror fieldName="name" theme="simple"/>
- <br />
- <s:property value="errors.name[0]"/>
- <s:debug></s:debug>
- </body>
context 把他看成周围的环境
例如: servletContext 即servlet 周围的环境 actionContext 即action周围的环境 及里面执行的过程或者一些东西
- package com.demo.action;
- import java.util.Map;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class LoginAction1 extends ActionSupport{
- private Map request;
- private Map session;
- private Map application;
- public LoginAction1 () {
- request = (Map) ActionContext.getContext().get("request");
- session = ActionContext.getContext().getSession();
- application = ActionContext.getContext().getApplication();
- }
- @Override
- public String execute() throws Exception {
- request.put("r1", "r1");
- session.put("s1", "s1");
- application.put("a1", "a1");
- return SUCCESS;
- }
- }
- // IOC 控制反转 常用这种方式
- package com.demo.action;
- import java.util.Map;
- import org.apache.struts2.interceptor.ApplicationAware;
- import org.apache.struts2.interceptor.RequestAware;
- import org.apache.struts2.interceptor.SessionAware;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
- private Map<String,Object> request;
- private Map<String,Object> session;
- private Map<String,Object> application;
- @Override
- public String execute() throws Exception {
- request.put("r1", "r1");
- session.put("s1", "s1");
- application.put("a1", "a1");
- return SUCCESS;
- }
- public void setRequest(Map<String, Object> request) {
- this.request = request;
- }
- public void setSession(Map<String, Object> session) {
- this.session = session;
- }
- public void setApplication(Map<String, Object> application) {
- this.application = application;
- }
- }
- package com.demo.action;
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class LoginAction3 extends ActionSupport{
- private HttpServletRequest request;
- private HttpSession session;
- private ServletContext application;
- public LoginAction3 () {
- request = ServletActionContext.getRequest();
- session = request.getSession();
- application = session.getServletContext();
- }
- @Override
- public String execute() throws Exception {
- request.setAttribute("r1", "r1");
- session.setAttribute("s1", "s1");
- application.setAttribute("a1", "a1");
- return SUCCESS;
- }
- }
- package com.demo.action;
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import org.apache.struts2.ServletActionContext;
- import org.apache.struts2.interceptor.ServletRequestAware;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class LoginAction4 extends ActionSupport implements ServletRequestAware{
- private HttpServletRequest request;
- private HttpSession session;
- private ServletContext application;
- @Override
- public String execute() throws Exception {
- request.setAttribute("r1", "r1");
- session.setAttribute("s1", "s1");
- application.setAttribute("a1", "a1");
- return SUCCESS;
- }
- public void setServletRequest(HttpServletRequest request) {
- this.request = request;
- this.session = request.getSession();
- this.application = session.getServletContext();
- }
- }
- <?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.devMode" value="true"/>
- <package name="login" extends="struts-default" namespace="/login">
- <action name="login*" class="com.demo.action.LoginAction{1}">
- <result>
- /user_login_success.jsp
- </result>
- </action>
- </package>
- </struts>
- <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
- <% String context = request.getContextPath(); %>
- <html>
- <head>
- </head>
- <body>
- 取得Map类型request,session,application,真实类型 HttpServletRequest, HttpSession, ServletContext的引用:
- <ol>
- <li>前三者: 依赖于容器</li>
- <li>前三者:IOC</li>(只有这种)
- <li>后三者: 依赖于容器</li>
- <li>后三者: IOC</li><br/>
- <form name="f" action="" method="post">
- 用户名: <input type="text" name="name"/>
- 密码: <input type="text" name="password"/><br/>
- <input type="button" value="submit1" onclick="javascript:document.f.action='login/login1';document.f.submit();"/>
- <input type="button" value="submit2" onclick="javascript:document.f.action='login/login2';document.f.submit();"/>
- <input type="button" value="submit3" onclick="javascript:document.f.action='login/login3';document.f.submit();"/>
- <input type="button" value="submit4" onclick="javascript:document.f.action='login/login4';document.f.submit();"/>
- </form>
- </ol>
- </body>
- </html>
- <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
- <%@taglib uri="/struts-tags" prefix="s" %>
- <html>
- <head>
- </head>
- <body>
- User Login Success ! <br/>
- <s:property value="#request.r1"/> | <%=request.getAttribute("r1") %> <br />
- <s:property value="#session.s1"/> | <%=session.getAttribute("s1") %> <br />
- <s:property value="#application.a1"/> | <%=application.getAttribute("a1") %> <br />
- <s:property value="#attr.a1"/><br>
- <s:property value="#attr.s1"/><br/>
- <s:property value="#attr.r1"/><br/>
- <s:debug></s:debug>
- </body>
- </html>
loginaction2
![](http://hi.csdn.net/attachment/201111/12/0_132110131731pn.gif)
- <?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>
- <package name="login" extends="struts=default" namespace="/login">
- <action name="login*" class="com.demo.action.LoginAction{1}">
- <result>/user_login_success.jsp</result>
- </action>
- </package>
- </struts>
- <pre name="code" class="html"><?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.devMode" value="true"/>
- <include file="login.xml"></include>
- </struts>
- </pre>把login.xml 文件包含到struts.xml 里面来<pre name="code" class="html"><pre name="code" class="html"><include file="login.xml"></include></pre><p></p>
- <pre></pre>
- 方便分成模块,便于开发<br>
- <pre></pre>
- <br>
- <pre name="code" class="html"><constant name="struts.devMode" value="true"/>
- <package name="front" extends="struts-default" namespace="/">
- <default-action-ref name="index"></default-action-ref>
- <action name="index">
- <result>
- /default.jsp
- </result>
- </action>
- </package>
- <pre name="code" class="html"><default-action-ref name="index"></default-action-ref> 如果访问不存在便 默认以index.action 为主 </pre><br>
- <pre></pre>
- <br>
- <img src="http://hi.csdn.net/attachment/201111/12/0_1321102675sPB4.gif" alt=""><br>
- <br>
- <p></p>
- <p></p>
- </pre></pre>