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="提交"/> <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="提交"/> <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>
原因:
解决方法:
时间: 2024-10-08 23:53:08