码迷,mamicode.com
首页 > 其他好文 > 详细

Action处理业务请求(一)

时间:2015-05-12 11:49:40      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

1、概述

技术分享

技术分享

 2、Action的作用

技术分享

技术分享

 3、第一种方式(设置私有属性)

log.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="login" method="post">
  用户名:<input type="text" name="userName"/>
  密码:<input type="password" name="password"/>
  <input type="submit" value="提交"/>&nbsp;&nbsp;
  <input type="reset" value="重置"/>
 </form>
</body>
</html>

loginSuccess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登陆成功,欢迎你:<s:property value="userName"/>
</body>
</html>

LoginAction类:

package com.ljb.web.action;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 第一种传递参数方法(设置私有属性,设置set与get方法)
 * @author Administrator
 *
 */
public class LoginAction extends ActionSupport {
 private String userName;
 private String password;
 
 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;
 }
 
 @Override
 public String execute() throws Exception {
  // TODO Auto-generated method stub
  return SUCCESS;
 }
}

struts.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>
        <!-- Add packages here -->
    <constant name="struts.devMode" value="true" />
 <package name="default" namespace="/" extends="struts-default">
       <action name="login" class="com.ljb.web.action.LoginAction">
            <result>
                /loginSuccess.jsp
            </result>
        </action>
    </package>
</struts>

技术分享

技术分享

 4、乱码问题

login.jsp

<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
 <form action="login" method="post">
  用户名:<input type="text" name="userName"/>
  密码:<input type="password" name="password"/>
  <input type="submit" value="提交"/>&nbsp;&nbsp;
  <input type="reset" value="重置"/>
 </form>
</body>
</html>

loginSuccess.jsp

<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
登陆成功,欢迎你:<s:property value="userName"/>
</body>
</html>

技术分享

技术分享

原因:

技术分享

 

解决方法:

技术分享

技术分享

 

 

 

 

Action处理业务请求(一)

标签:

原文地址:http://my.oschina.net/u/2320342/blog/413600

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!