第一个struts2程序
2013-11-28 21:10
274 查看
搭建struts2
引入struts的jar包
这可以自己下载。然后在lib下找
web.xml放在WEB-INF下
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts</display-name>
<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>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts放在src下
<?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">
<package name="default" namespace="/" extends="struts-default">
<action name="student" class="com.ljq.action.studentAction">
<!--<result type="studentAction">
<param name="error">error.jsp</param>
<param name="welcome">welcome.jsp</param>
<param name="index">index.jsp</param>
</result>
-->
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
编写java代码
studentAction
package com.ljq.action;
public class studentAction {
private int stuId;
private String name;
private String password;
private String sex;
private int age;
public String execute(){
/*System.out.println("stuId"+stuId);
System.out.println("name"+name);
System.out.println("password"+password);
System.out.println("sex"+sex);
System.out.println("age"+age);*/
if("ljq".equals(getName())&&"123".equals(getPassword())){
return "success";
}
else{
return "error";
}
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
login.jsp
<body >
<h1>欢迎使用信息学院信息管理系统学院</h1>
<form action="student.action" method="post">
用户名<input type="text" name="name"/><br/>
密码<input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</form>
</body>
error.jsp
<body>
<h1>您输入的信息错误,请重新登陆</h1>
<form action="student" method="post"></form>
用户名<input type="text" name="name"/><br/>
密码<input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</form>
</body>
welcome.jsp
<body>
登录成功,欢迎使用
</body>
经过以上的工作,整个代码就可运行了。
/*有啥问题大家一起交流哦*/
引入struts的jar包
这可以自己下载。然后在lib下找
web.xml放在WEB-INF下
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts</display-name>
<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>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts放在src下
<?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">
<package name="default" namespace="/" extends="struts-default">
<action name="student" class="com.ljq.action.studentAction">
<!--<result type="studentAction">
<param name="error">error.jsp</param>
<param name="welcome">welcome.jsp</param>
<param name="index">index.jsp</param>
</result>
-->
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
编写java代码
studentAction
package com.ljq.action;
public class studentAction {
private int stuId;
private String name;
private String password;
private String sex;
private int age;
public String execute(){
/*System.out.println("stuId"+stuId);
System.out.println("name"+name);
System.out.println("password"+password);
System.out.println("sex"+sex);
System.out.println("age"+age);*/
if("ljq".equals(getName())&&"123".equals(getPassword())){
return "success";
}
else{
return "error";
}
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
login.jsp
<body >
<h1>欢迎使用信息学院信息管理系统学院</h1>
<form action="student.action" method="post">
用户名<input type="text" name="name"/><br/>
密码<input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</form>
</body>
error.jsp
<body>
<h1>您输入的信息错误,请重新登陆</h1>
<form action="student" method="post"></form>
用户名<input type="text" name="name"/><br/>
密码<input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</form>
</body>
welcome.jsp
<body>
登录成功,欢迎使用
</body>
经过以上的工作,整个代码就可运行了。
/*有啥问题大家一起交流哦*/
相关文章推荐
- Struts2系列教程1:第一个Struts2程序
- 我的第一个基于MyEclipse下的Struts2运用程序
- 第一个struts2程序
- 第一个Struts2程序之HelloWorld
- 第一个struts2程序
- 第一个Struts2程序,包含编码设置以及开发模式
- 第一个Struts2程序,弄了一下午
- 手动编写第一个struts2程序
- struts2的配置与第一个测试程序
- 第一个Struts2程序,弄了一下午
- 重温struts2之第一个struts2程序helloworld
- Struts2教程1:第一个Struts2程序
- 第一个Struts2程序,弄了一下午
- struts2 的第一个程序:登陆页面
- Struts2的第一个程序
- Struts2教程1:第一个Struts2程序 ----很不错的入门文章
- 【SSH三大框架】Struts2基础第一篇:编写第一个Struts2程序
- 第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- 1、第一个Struts2程序