以下struts搭建 参考http://www.cnblogs.com/sunliming/archive/2010/10/04/1842114.html
1eclipse web项目搭建
以前一直是用myeclipse发布web项目的,没有用过eclipse发布过web项目,eclipse的web项目都是从svn直接下来下的。因为最近刚好接触了一个ssh的项目,又刚好弄完了工作任务,就想着自己搭一个ssh的框架。本来想从当前这个项目上切出来的,然后似乎工作量很大,并且自己对ssh还不是很了解,所以还是先从网上一步一步来。
一开始就坎坷不断,新建的web项目没有web.xml文件,强迫症发作,必须有..所以百度下,发现原来是自己没有勾选创建web.xml,直接创建项目了。
创建步骤如下,直接上图,会的请无视
这里的勾选就是生成xml文件。
然而通过eclipse发布web项目又不行,一开始以为是下面的错误造成的
The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path
换了tomcat,加了环境变量也不行,最后还是放弃了,直接百度eclipse发布tomcat 404结果是tomcat server配置的问题,
server location应该选择use tomcat installation,但是一开始无法修改server location,后来发现是我改了tomcat server 的properties的location应该是下图
我选择了
双击 tomcat server下图
出现
选择server location的第二个选择就行
重新发布启动 ok
2 s项目搭建
完整的struts的项目结构图,只有struts目前
struts所需的jar如图,
web.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns=
"http://java.sun.com/xml/ns/javaee"
xmlns:web=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
-
xsi:schemaLocation=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
-
id=
"WebApp_ID"
version=
"3.0">
-
<display-name>ssh
</display-name>
-
<welcome-file-list>
-
<welcome-file>index.html
</welcome-file>
-
<welcome-file>index.htm
</welcome-file>
-
<welcome-file>index.jsp
</welcome-file>
-
<welcome-file>default.html
</welcome-file>
-
<welcome-file>default.htm
</welcome-file>
-
<welcome-file>default.jsp
</welcome-file>
-
</welcome-file-list>
-
-
<!-- 下面这段代码是struts2所固定的不需要更改 -->
-
<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>
-
</web-app>
struts.xml
action如果没指定方法默认执行java类的execute方法
-
<?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="struts2" extends="struts-default">
-
<action name="login" class="com.Login">
-
<result name="success" type="redirect">index.jsp
</result>
-
<result name="input">login.jsp
</result>
-
<result name="error">login.jsp
</result>
-
</action>
-
</package>
-
</struts>
login.jsp
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%@ taglib prefix="s" uri="/struts-tags" %>
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<title>登录
</title>
-
<meta http-equiv="pragma" content="no-cache">
-
<meta http-equiv="cache-control" content="no-cache">
-
<meta http-equiv="expires" content="0">
-
</head>
-
<body>
-
<!-- 这里的action="login.action" 和action="login"一样的效果 -->
-
<s:form name="form1" action="login" >
-
<s:textfield name="username" label="username" >
</s:textfield>
-
<s:password name="password" label="password" >
</s:password>
-
<s:submit label="submit">
</s:submit>
-
</s:form>
-
<s:actionerror/>
-
</body>
-
</html>
以下标签头支持<context:annotation-config /><context:component-scan base-package="com" />
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop=
"http://www.springframework.org/schema/aop"
-
xmlns:context=
"http://www.springframework.org/schema/context"
xmlns:tx=
"http://www.springframework.org/schema/tx"
-
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context-2.5.xsd
-
http://www.springframework.org/schema/aop
-
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
-
http://www.springframework.org/schema/tx
-
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
3 ssh项目整合
![](https://i-blog.csdnimg.cn/blog_migrate/1dcce7e4131209473835e9b4b26d5572.png)
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns=
"http://java.sun.com/xml/ns/javaee"
xmlns:web=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
-
xsi:schemaLocation=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
-
id=
"WebApp_ID"
version=
"3.0">
-
<display-name>ssh2_test
</display-name>
-
<welcome-file-list>
-
<welcome-file>index.jsp
</welcome-file>
-
</welcome-file-list>
-
-
<context-param>
-
<param-name>contextConfigLocation
</param-name>
-
<param-value>classpath:config/applicationContext-*.xml
</param-value>
-
</context-param>
-
-
-
<!-- 将HibernateSession开关控制配置在Filter,保证一个请求一个session,并对lazy提供支持 -->
-
<!-- <filter>
-
<filter-name>OpenSessionInView</filter-name>
-
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
-
<init-param>
-
<param-name>singleSession</param-name>
-
<param-value>true</param-value>
-
</init-param>
-
-
</filter>
-
-
<filter-mapping>
-
<filter-name>OpenSessionInView</filter-name>
-
<url-pattern>/*</url-pattern>
-
</filter-mapping> -->
-
-
<!-- 配置Struts2 -->
-
<filter>
-
<filter-name>struts2
</filter-name>
-
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
-
<init-param>
-
<param-name>config
</param-name>
-
<param-value>struts-default.xml,struts-plugin.xml,/config/struts.xml
</param-value>
-
</init-param>
-
</filter>
-
-
<filter-mapping>
-
<filter-name>struts2
</filter-name>
-
<url-pattern>/*
</url-pattern>
-
</filter-mapping>
-
-
-
<!-- 配置spring -->
-
<listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
-
</listener>
-
-
</web-app>
具体代码如下:
-
<?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="struts2" extends="struts-default">
-
<action name="login" class="loginAction">
-
<result name="success" type="redirect">index.jsp
</result>
-
<result name="input">login.jsp
</result>
-
<result name="error">login.jsp
</result>
-
<!-- 展示用户信息页面 -->
-
<result name="show">show.jsp
</result>
-
</action>
-
</package>
-
</struts>
applicationContext-common.xml主要用来配置数据源,sessionFactory,事务之类的,代码里有详细注释了
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop=
"http://www.springframework.org/schema/aop"
-
xmlns:context=
"http://www.springframework.org/schema/context"
xmlns:tx=
"http://www.springframework.org/schema/tx"
-
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
-
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
-
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
-
-
-
<!--配数据源 -->
-
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
-
destroy-method=
"close">
-
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
-
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
-
<property name="username" value="root" />
-
<property name="password" value="123456" />
-
</bean>
-
-
<!-- 配置session工厂 -->
-
<bean id="sessionFactory"
-
class=
"org.springframework.orm.hibernate4.LocalSessionFactoryBean">
-
<property name="dataSource" ref="dataSource" />
-
-
<property name="hibernateProperties">
-
<props>
-
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</prop>
-
<prop key="hibernate.show_sql">true
</prop>
-
<prop key="hibernate.hbm2ddl.auto">update
</prop>
-
-
</props>
-
</property>
-
<!-- 如果使用配置文件 -->
-
<!-- <property name="mappingLocations"> <list> <value>classpath:com/jialin/entity/User.hbm.xml</value>
-
</list> </property> -->
-
<property name="annotatedClasses">
-
<list>
-
<value>com.user.entity.User
</value>
-
</list>
-
</property>
-
-
</bean>
-
-
-
<!-- 配置事务管理器 -->
-
<bean id="transactionManager"
-
class=
"org.springframework.orm.hibernate4.HibernateTransactionManager">
-
<property name="sessionFactory" ref="sessionFactory" />
-
</bean>
-
-
<!-- 事务的传播特性 -->
-
<tx:advice id="txadvice" transaction-manager="transactionManager">
-
<tx:attributes>
-
<tx:method name="add*" propagation="REQUIRED" />
-
<tx:method name="delete*" propagation="REQUIRED" />
-
<tx:method name="get*" propagation="REQUIRED" />
-
<tx:method name="modify*" propagation="REQUIRED" />
-
<tx:method name="*" propagation="REQUIRED" />
-
<!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 -->
-
<!-- <tx:method name="*" propagation="REQUIRED" read-only="true" /> -->
-
</tx:attributes>
-
</tx:advice>
-
-
<!-- 那些类那些方法使用事务 -->
-
<aop:config>
-
<!-- 只对业务逻辑层实施事务 -->
-
<aop:pointcut id="allManagerMethod" expression="(execution(* com.*.dao.*.*(..)))" />
-
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txadvice" />
-
</aop:config>
-
<context:annotation-config />
-
-
-
<!-- 需要扫描注入的包-->
-
<context:component-scan base-package="com" />
-
-
</beans>
几个主要的文件
login.jsp
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%@ taglib prefix="s" uri="/struts-tags"%>
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<title>登录
</title>
-
<meta http-equiv="pragma" content="no-cache">
-
<meta http-equiv="cache-control" content="no-cache">
-
<meta http-equiv="expires" content="0">
-
</head>
-
<body>
-
<!-- 这里的action="login.action" 和action="login"一样的效果 -->
-
<s:form name="form1" action="login">
-
<s:textfield name="username" label="username">
</s:textfield>
-
<s:password name="password" label="password">
</s:password>
-
<s:submit label="submit">
</s:submit>
-
</s:form>
-
<s:actionerror />
-
</form>
-
</body>
-
</html>
-
package com.user.action;
-
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Controller;
-
-
import com.user.entity.User;
-
-
@Controller(
"loginAction")
-
public
class UserController {
-
public String username;
-
public String password;
-
public User user;
-
public String id;
-
-
@Autowired
-
public com.user.service.UserService service;
-
-
public String execute() {
-
User user=
new User();
-
user.setAge(
1);
-
user.setName(
"2");
-
user.setPassword(
"3");
-
service.addUser(user);
-
return
"success";
-
}
-
-
public void deleteUser(){
-
service.deleteUser(id);
-
}
-
-
-
public String showUser(){
-
user=service.findById(
"8a88ce795606ebd4015606ec454c0000");
-
return
"show";
-
}
-
-
public void updateUser(){
-
user.setAge(
222);
-
service.updateUser(user);
-
}
-
-
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;
-
}
-
-
public User getUser() {
-
return user;
-
}
-
-
public void setUser(User user) {
-
this.user = user;
-
}
-
-
public String getId() {
-
return id;
-
}
-
-
public void setId(String id) {
-
this.id = id;
-
}
-
-
-
}
访问主页为:项目名/login.jsp然后点击submit按钮为增加用户,
项目名/login!showUser 为展示用户的age
项目名/login!updateUser为修改刚才展示用户的age设置为222
项目名/login!deleteUser?id=8a88ce795606d7c0015606d805c50000 id后面跟你要删除的用户id 为删除
用户表为p_user 字段id,name,password,age
如需源码,戳这里点击打开链接